Skip to content

Commit

Permalink
feat: Render entry.go (first stage) (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech authored Mar 8, 2024
1 parent ab72553 commit cdb02a7
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 10 deletions.
6 changes: 6 additions & 0 deletions cmd/mktp/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ output:
go_sdk: "../generated/pango"
terraform_provider: "../generated/terraform-provider-panos"
assets:
# generic_package:
# source: "assets/generic"
# target:
# go_sdk: true
# terraform_provider: false
# destination: "generic"
# util_package:
# source: "assets/util"
# target:
Expand Down
10 changes: 7 additions & 3 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@ func (c *Creator) generateOutputFileFromTemplate(tmpl *template.Template, output
func (c *Creator) parseTemplate(templateName string) (*template.Template, error) {
templatePath := fmt.Sprintf("%s/%s", c.TemplatesDir, templateName)
funcMap := template.FuncMap{
"packageName": translate.PackageName,
"locationType": translate.LocationType,
"omitEmpty": translate.OmitEmpty,
"packageName": translate.PackageName,
"locationType": translate.LocationType,
"specParamType": translate.SpecParamType,
"omitEmpty": translate.OmitEmpty,
"contains": func(full, part string) bool {
return strings.Contains(full, part)
},
"subtract": func(a, b int) int {
return a - b
},
"asEntryXpath": translate.AsEntryXpath,
}
tmpl, err := template.New(templateName).Funcs(funcMap).ParseFiles(templatePath)
Expand Down
16 changes: 16 additions & 0 deletions pkg/naming/names_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package naming

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestCamelCase(t *testing.T) {
assert.Equal(t, "CamelCase", CamelCase("", "camel_case", "", true))
assert.Equal(t, "camelCase", CamelCase("", "camel_case", "", false))
}

func TestAlphaNumeric(t *testing.T) {
assert.Equal(t, "alphanumeric", AlphaNumeric("alpha_numeric"))
assert.Equal(t, "AlphaNumeric", AlphaNumeric("Alpha_Numeric"))
}
64 changes: 62 additions & 2 deletions pkg/properties/normalized.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ type Spec struct {
}

type SpecParam struct {
Name *NameVariant
Description string `json:"description" yaml:"description"`
Type string `json:"type" yaml:"type"`
Required bool `json:"required" yaml:"required"`
Length *SpecParamLength `json:"length" yaml:"length,omitempty"`
Count *SpecParamCount `json:"count" yaml:"count,omitempty"`
Items *SpecParamItems `json:"items" yaml:"items,omitempty"`
Expand Down Expand Up @@ -140,12 +142,14 @@ func ParseSpec(input []byte) (*Normalization, error) {

err := content.Unmarshal(input, &spec)

err = spec.AddNameVariants()
err = spec.AddNameVariantsForLocation()
err = spec.AddNameVariantsForParams()
err = spec.AddDefaultTypesForParams()

return &spec, err
}

func (spec *Normalization) AddNameVariants() error {
func (spec *Normalization) AddNameVariantsForLocation() error {
for key, location := range spec.Locations {
location.Name = &NameVariant{
Underscore: key,
Expand All @@ -163,6 +167,62 @@ func (spec *Normalization) AddNameVariants() error {
return nil
}

func AddNameVariantsForParams(name string, param *SpecParam) error {
param.Name = &NameVariant{
Underscore: name,
CamelCase: naming.CamelCase("", name, "", true),
}
if param.Spec != nil {
for key, childParam := range param.Spec.Params {
if err := AddNameVariantsForParams(key, childParam); err != nil {
return err
}
}
for key, childParam := range param.Spec.OneOf {
if err := AddNameVariantsForParams(key, childParam); err != nil {
return err
}
}
}
return nil
}

func (spec *Normalization) AddNameVariantsForParams() error {
if spec.Spec != nil {
for key, param := range spec.Spec.Params {
if err := AddNameVariantsForParams(key, param); err != nil {
return err
}
}
for key, param := range spec.Spec.OneOf {
if err := AddNameVariantsForParams(key, param); err != nil {
return err
}
}
}
return nil
}

func (spec *Normalization) AddDefaultTypesForParams() error {
if spec.Spec != nil {
if spec.Spec.Params != nil {
for _, param := range spec.Spec.Params {
if param.Type == "" {
param.Type = "string"
}
}
}
if spec.Spec.OneOf != nil {
for _, param := range spec.Spec.OneOf {
if param.Type == "" {
param.Type = "string"
}
}
}
}
return nil
}

func (spec *Normalization) Sanity() error {
if spec.Name == "" {
return errors.New("name is required")
Expand Down
32 changes: 28 additions & 4 deletions pkg/properties/normalized_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,12 @@ version: 10.1.0
spec:
params:
description:
name:
underscore: description
camelcase: Description
description: The description.
type: string
required: false
length:
min: 0
max: 1023
Expand All @@ -263,8 +267,12 @@ spec:
- description
spec: null
tags:
name:
underscore: tags
camelcase: Tags
description: The administrative tags.
type: list
required: false
count:
min: null
max: 64
Expand All @@ -280,8 +288,12 @@ spec:
spec: null
one_of:
fqdn:
name:
underscore: fqdn
camelcase: Fqdn
description: The FQDN value.
type: ""
type: string
required: false
length:
min: 1
max: 255
Expand All @@ -291,22 +303,34 @@ spec:
- fqdn
spec: null
ip_netmask:
name:
underscore: ip_netmask
camelcase: IpNetmask
description: The IP netmask value.
type: ""
type: string
required: false
profiles:
- xpath:
- ip-netmask
spec: null
ip_range:
name:
underscore: ip_range
camelcase: IpRange
description: The IP range value.
type: ""
type: string
required: false
profiles:
- xpath:
- ip-range
spec: null
ip_wildcard:
name:
underscore: ip_wildcard
camelcase: IpWildcard
description: The IP wildcard value.
type: ""
type: string
required: false
profiles:
- xpath:
- ip-wildcard
Expand Down
19 changes: 19 additions & 0 deletions pkg/translate/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ func LocationType(location *properties.Location, pointer bool) string {
}
}

func SpecParamType(param *properties.SpecParam) string {
prefix := ""
if !param.Required {
prefix = "*"
}
if param.Type == "list" {
prefix = "[]"
}

calculatedType := ""
if param.Type == "list" && param.Items != nil {
calculatedType = param.Items.Type
} else {
calculatedType = param.Type
}

return prefix + calculatedType
}

func OmitEmpty(location *properties.Location) string {
if location.Vars != nil {
return ",omitempty"
Expand Down
28 changes: 28 additions & 0 deletions pkg/translate/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,34 @@ func TestLocationType(t *testing.T) {
assert.Contains(t, locationTypes, "bool")
}

func TestSpecParamType(t *testing.T) {
// given
paramTypeRequiredString := properties.SpecParam{
Type: "string",
Required: true,
}
itemsForParam := properties.SpecParamItems{
Type: "string",
}
paramTypeListString := properties.SpecParam{
Type: "list",
Items: &itemsForParam,
}
paramTypeOptionalString := properties.SpecParam{
Type: "string",
}

// when
calculatedTypeRequiredString := SpecParamType(&paramTypeRequiredString)
calculatedTypeListString := SpecParamType(&paramTypeListString)
calculatedTypeOptionalString := SpecParamType(&paramTypeOptionalString)

// then
assert.Equal(t, "string", calculatedTypeRequiredString)
assert.Equal(t, "[]string", calculatedTypeListString)
assert.Equal(t, "*string", calculatedTypeOptionalString)
}

func TestOmitEmpty(t *testing.T) {
// given
yamlParsedData, _ := properties.ParseSpec([]byte(sampleSpec))
Expand Down
70 changes: 69 additions & 1 deletion templates/sdk/entry.tmpl
Original file line number Diff line number Diff line change
@@ -1 +1,69 @@
package {{packageName .GoSdkPath}}
package {{packageName .GoSdkPath}}

import (
"encoding/xml"
"fmt"

"github.com/PaloAltoNetworks/pango/filtering"
"github.com/PaloAltoNetworks/pango/generic"
"github.com/PaloAltoNetworks/pango/util"
"github.com/PaloAltoNetworks/pango/version"
)

var (
_ filtering.Fielder = &Entry{}
)

var (
Suffix = []string{
{{- $length := subtract (len .XpathSuffix) 1 }}
{{- range $index, $suffix := .XpathSuffix}}"
{{- $suffix}}"{{- if lt $index $length}},{{- end}}
{{- end}}}
)

type Entry struct {
{{- if .Entry}}
Name string
{{- end}}
{{- range $_, $param := .Spec.Params}}
{{$param.Name.CamelCase}} {{specParamType $param}}
{{- end}}
{{- range $_, $param := .Spec.OneOf}}
{{$param.Name.CamelCase}} {{specParamType $param}}
{{- end}}

Misc map[string][]generic.Xml
}

func (e *Entry) CopyMiscFrom(v *Entry) {
if v == nil || len(v.Misc) == 0 {
return
}

e.Misc = make(map[string][]generic.Xml)
for key := range v.Misc {
e.Misc[key] = append([]generic.Xml(nil), v.Misc[key]...)
}
}

func (e *Entry) Field(v string) (any, error) {
{{- if .Entry}}
if v == "name" || v == "Name" {
return e.Name, nil
}
{{- end}}

{{- range $_, $param := .Spec.Params}}
if v == "{{$param.Name.Underscore}}" || v == "{{$param.Name.CamelCase}}" {
return e.{{$param.Name.CamelCase}}, nil
}
{{- end}}
{{- range $_, $param := .Spec.OneOf}}
if v == "{{$param.Name.Underscore}}" || v == "{{$param.Name.CamelCase}}" {
return e.{{$param.Name.CamelCase}}, nil
}
{{- end}}

return nil, fmt.Errorf("unknown field")
}

0 comments on commit cdb02a7

Please sign in to comment.