Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Render entry.go (first stage) #31

Merged
merged 36 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
2155e70
new function to make indentation equal in list of strings
sebastianczech Mar 4, 2024
d5ffed6
execute new function creating definition of structs
sebastianczech Mar 4, 2024
dcb8f2e
new function creating definition of structs (with tests)
sebastianczech Mar 4, 2024
7c5f19b
fix logic after tests
sebastianczech Mar 4, 2024
8171827
refactor code (remove duplication and extract func)
sebastianczech Mar 4, 2024
7aad155
fix random order in keys for maps
sebastianczech Mar 5, 2024
b2bef13
update testify package
sebastianczech Mar 5, 2024
cd267f2
new functions to generate functions for locations
sebastianczech Mar 5, 2024
e975329
use new functions for locations
sebastianczech Mar 5, 2024
b5095ac
extend template, do not concat strings to generate structs
sebastianczech Mar 5, 2024
a07ae67
extend template, do not concat strings to generate functions
sebastianczech Mar 5, 2024
2b7514c
clean code
sebastianczech Mar 5, 2024
5bc88c5
tests for functions used while generating structs and funcs
sebastianczech Mar 5, 2024
d9fdd83
fix issues after tests
sebastianczech Mar 5, 2024
f1f2525
remove not required spaces from templates
sebastianczech Mar 5, 2024
b2fb714
introduce new struct NameVariant for Location names and variables, ge…
sebastianczech Mar 6, 2024
b535826
Change Go SDK path for security policy rule
sebastianczech Mar 6, 2024
ea21716
extend configuration file with settings for copying static assets
sebastianczech Mar 6, 2024
d2424f3
static files from pango in develop branch
sebastianczech Mar 6, 2024
0824d64
add function to copy static assets
sebastianczech Mar 6, 2024
54f99e3
change go sdk path for service and address group
sebastianczech Mar 6, 2024
f10d323
Merge branch 'render-location' into copy-static-files
sebastianczech Mar 6, 2024
f34380e
Add generic.Xml
sebastianczech Mar 7, 2024
73a3d43
generate part of entry.go - struct Entry
sebastianczech Mar 7, 2024
a3b31d9
add tests for names
sebastianczech Mar 7, 2024
5b07e8b
add generic package
sebastianczech Mar 7, 2024
2a8705c
add name variants for params
sebastianczech Mar 7, 2024
7b75458
start work on entry.go template
sebastianczech Mar 7, 2024
96e300a
add error handling for func AsEntryXpath
sebastianczech Mar 8, 2024
4b35c89
Merge branch 'render-location' into copy-static-files
sebastianczech Mar 8, 2024
82b673b
rename assets.go
sebastianczech Mar 8, 2024
d67d35c
ignore error while closing file in defer
sebastianczech Mar 8, 2024
f1f1e8b
remove files copied from pango
sebastianczech Mar 8, 2024
8f39f38
merge copy-static-files branch
sebastianczech Mar 8, 2024
dee1854
remove files copied from pango
sebastianczech Mar 8, 2024
e16d0d6
merge main
sebastianczech Mar 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
Loading