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 service.go for entry and config #42

Merged
merged 72 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 69 commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
ad7b989
add EntryXml struct, render all missing functions for entry.go, exten…
sebastianczech Mar 11, 2024
948fe24
start work on service.go
sebastianczech Mar 11, 2024
024e86b
refactor names in template for entry.go
sebastianczech Mar 11, 2024
9b1a666
add not_present and from_version to SpecParamProfile
sebastianczech Mar 11, 2024
5c148fc
current service is only prepared for entry
sebastianczech Mar 11, 2024
dd3d1db
clear service template
sebastianczech Mar 11, 2024
2fb79ed
render entry.go only for specs with entry
sebastianczech Mar 11, 2024
8372889
create structs for nested specs
sebastianczech Mar 11, 2024
d305093
generate content of function SpecifyEntry for specification with nest…
sebastianczech Mar 12, 2024
9bbe3e1
generate content of function Normalize for specification with nested …
sebastianczech Mar 12, 2024
2180a57
add function comments
sebastianczech Mar 12, 2024
f1f9015
replace all string concatenation using "+" by function fmt.Sprintf()
sebastianczech Mar 13, 2024
5966f1b
for item with type object, render []string, which will contain all me…
sebastianczech Mar 13, 2024
f7bb754
refactor NormalizeAssignment(), SpecifyEntryAssignment(), fix some of…
sebastianczech Mar 13, 2024
3949bb7
new template to generate config.go
sebastianczech Mar 13, 2024
6ae47fc
extend function NormalizeAssignment() and SpecifyEntryAssignment(), w…
sebastianczech Mar 13, 2024
802698c
fix tests
sebastianczech Mar 13, 2024
1468446
extend template functions after tests for service.yaml
sebastianczech Mar 13, 2024
dbeedb4
extend template functions
sebastianczech Mar 13, 2024
9ccdce1
apply changes after review (#1)
sebastianczech Mar 14, 2024
c40fbfd
remove comments
sebastianczech Mar 14, 2024
f3e7d56
use fmt.Errorf to return errors
sebastianczech Mar 14, 2024
e7fa93a
apply changes after review (#2)
sebastianczech Mar 14, 2024
b5e271e
refactor SpecMatchesFunction()
sebastianczech Mar 14, 2024
633940c
refactor funcs.go
sebastianczech Mar 15, 2024
57bec84
refactor structs.go
sebastianczech Mar 15, 2024
c9640fe
merge changes from render-entry branch
sebastianczech Mar 15, 2024
ed7ed72
remove nested variables declaration, use the directly in assignments
sebastianczech Mar 18, 2024
24f1f51
fix problems while using entry.go with E2E tests for address object
sebastianczech Mar 19, 2024
44db76f
fix problems with default type for param, which has spec
sebastianczech Mar 19, 2024
5a64220
restore nested declaration (need to rebuild that mechanism)
sebastianczech Mar 20, 2024
5ec29bb
rebuild mechanism of generating nested objects
sebastianczech Mar 20, 2024
4fd868a
fix location template
sebastianczech Mar 20, 2024
3fedad1
render 'Misc []generic.Xml `xml:",any"' for child objects (not only f…
sebastianczech Mar 22, 2024
e964878
render 'Misc []generic.Xml `xml:",any"' for child objects (not only f…
sebastianczech Mar 22, 2024
b399cf1
set value for nested Misc
sebastianczech Mar 22, 2024
c267fff
fix tests after changes with Misc
sebastianczech Mar 22, 2024
e11fdca
struct ending with Xml are no more public from now
sebastianczech Mar 22, 2024
fe77494
introduce function to detecting versions
sebastianczech Mar 22, 2024
2f16fb8
return unique versions in format required in template
sebastianczech Mar 22, 2024
a9331b0
render different structs names for different versions
sebastianczech Mar 22, 2024
3ab0edd
improve rendering different structs names for different versions
sebastianczech Mar 22, 2024
279b05a
Introduce version struct
sebastianczech Mar 25, 2024
61a505d
Continue work on versioning
sebastianczech Mar 25, 2024
10fc1ec
Remove version package
sebastianczech Mar 25, 2024
9a9099b
Restore version package
sebastianczech Mar 25, 2024
68cb7c5
Check if param is supported in specific PAN-OS version
sebastianczech Mar 25, 2024
2f86bd6
Extend function for checking if param is supported in specific PAN-OS…
sebastianczech Mar 25, 2024
6ca7bbf
Use function paramSupportedInVersion in templates
sebastianczech Mar 25, 2024
2423da7
Do not render assignment for not supported param in specific version
sebastianczech Mar 25, 2024
e49158a
Update template to use newer versions of functions
sebastianczech Mar 25, 2024
9ac4470
Refactor code
sebastianczech Mar 25, 2024
862bd4a
Fix config template after tests for NTP
sebastianczech Mar 26, 2024
f284c78
Fix config template after tests for DNS
sebastianczech Mar 26, 2024
337c2ed
Remove CopyMiscFrom, SpecMatches from config template
sebastianczech Mar 26, 2024
644db51
Add comment for MarshalXML in config template
sebastianczech Mar 26, 2024
9377a74
Working SpecMatches() (not refactored)
sebastianczech Mar 27, 2024
0877c79
Refactor code to render SpecMatches()
sebastianczech Mar 27, 2024
b78ffd0
Fix versioning after more tests
sebastianczech Mar 27, 2024
782d829
Fix Misc map[string][]generic.Xml
sebastianczech Mar 28, 2024
583b48d
Make specify functions internal (we have multiple versions of specify…
sebastianczech Mar 28, 2024
b331865
Remove CopyMiscFrom(), fix issues with nested & versioned params
sebastianczech Mar 29, 2024
f86c660
Fixes in location.tmpl after tests while building service.go
sebastianczech Apr 2, 2024
47f7d07
Render service.go for entry and config
sebastianczech Apr 2, 2024
b272b01
Rename function XmlPathSuffixes()
sebastianczech Apr 2, 2024
ec3c02e
Fix problem with nested specs and versions
sebastianczech Apr 2, 2024
7abbcd2
Use imports manager
sebastianczech Apr 3, 2024
1399b18
Fix blank lines
sebastianczech Apr 3, 2024
2a7e485
Add missing Target: s.client.GetTarget()
sebastianczech Apr 3, 2024
d31fe9c
Do not introduce short names for imports
sebastianczech Apr 3, 2024
03809ff
Do not introduce short names for imports
sebastianczech Apr 3, 2024
ce81843
Add missing Target: s.client.GetTarget(),
sebastianczech Apr 4, 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
2 changes: 2 additions & 0 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (c *Creator) createFile(filePath string) (*os.File, error) {
func (c *Creator) parseTemplate(templateName string) (*template.Template, error) {
templatePath := filepath.Join(c.TemplatesDir, templateName)
funcMap := template.FuncMap{
"renderImports": translate.RenderImports,
"packageName": translate.PackageName,
"locationType": translate.LocationType,
"specParamType": translate.SpecParamType,
Expand All @@ -138,6 +139,7 @@ func (c *Creator) parseTemplate(templateName string) (*template.Template, error)
"nestedSpecs": translate.NestedSpecs,
"createGoSuffixFromVersion": translate.CreateGoSuffixFromVersion,
"paramSupportedInVersion": translate.ParamSupportedInVersion,
"xmlPathSuffixes": translate.XmlPathSuffixes,
}
return template.New(templateName).Funcs(funcMap).ParseFiles(templatePath)
}
11 changes: 11 additions & 0 deletions pkg/translate/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,14 @@ func argumentTypeForSpecMatchesFunction(parent []string, param *properties.SpecP
strings.Join(parent, ""), param.Name.CamelCase)
}
}

// XmlPathSuffixes return XML path suffixes created from profiles.
func XmlPathSuffixes(param *properties.SpecParam) []string {
xmlPathSuffixes := []string{}
if param.Profiles != nil {
for _, profile := range param.Profiles {
xmlPathSuffixes = append(xmlPathSuffixes, strings.Join(profile.Xpath, "/"))
}
}
return xmlPathSuffixes
}
24 changes: 24 additions & 0 deletions pkg/translate/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,27 @@ return true
// then
assert.Equal(t, expectedNestedSpec, renderedNestedSpecMatches)
}

func TestXmlPathSuffixes(t *testing.T) {
// given
spec := properties.Spec{
Params: map[string]*properties.SpecParam{
"a": {
Profiles: []*properties.SpecParamProfile{{
Xpath: []string{"test", "a"},
}},
Name: &properties.NameVariant{
Underscore: "a",
CamelCase: "A",
},
},
},
}
expectedXpathSuffixes := []string{"test/a"}

// when
actualXpathSuffixes := XmlPathSuffixes(spec.Params["a"])

// then
assert.Equal(t, expectedXpathSuffixes, actualXpathSuffixes)
}
31 changes: 31 additions & 0 deletions pkg/translate/imports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package translate

import "github.com/paloaltonetworks/pan-os-codegen/pkg/imports"

func RenderImports(templateType string) (string, error) {
manager := imports.NewManager()

switch templateType {
case "entry":
manager.AddStandardImport("encoding/xml", "")
manager.AddStandardImport("fmt", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/filtering", "filtering")
sebastianczech marked this conversation as resolved.
Show resolved Hide resolved
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/generic", "generic")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/util", "util")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/version", "version")
case "location":
manager.AddStandardImport("fmt", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/errors", "errors")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/util", "util")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/version", "version")
case "service":
manager.AddStandardImport("context", "")
manager.AddStandardImport("fmt", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/errors", "errors")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/filtering", "filtering")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/util", "util")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/xmlapi", "xmlapi")
}

return manager.RenderImports()
}
25 changes: 25 additions & 0 deletions pkg/translate/imports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package translate

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

func TestRenderImports(t *testing.T) {
// given
expectedImports := `
import (
"fmt"

errors "github.com/PaloAltoNetworks/pango/errors"
util "github.com/PaloAltoNetworks/pango/util"
version "github.com/PaloAltoNetworks/pango/version"
)`

// when
actualImports, _ := RenderImports("location")

// then
assert.NotNil(t, actualImports)
assert.Equal(t, expectedImports, actualImports)
}
10 changes: 1 addition & 9 deletions templates/sdk/entry.tmpl
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
{{- if .Entry}}
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"
)
{{renderImports "entry"}}

var (
_ filtering.Fielder = &Entry{}
Expand Down
8 changes: 1 addition & 7 deletions templates/sdk/location.tmpl
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package {{packageName .GoSdkPath}}

import (
"fmt"

"github.com/PaloAltoNetworks/pango/errors"
"github.com/PaloAltoNetworks/pango/util"
"github.com/PaloAltoNetworks/pango/version"
)
{{renderImports "location"}}

type Location struct {
{{range $key, $location := .Locations}}
Expand Down
Loading