Skip to content

Commit

Permalink
feat: Render service.go for entry and config (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech authored Apr 4, 2024
1 parent 49457a5 commit ba3131c
Show file tree
Hide file tree
Showing 8 changed files with 653 additions and 17 deletions.
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", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/generic", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/util", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/version", "")
case "location":
manager.AddStandardImport("fmt", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/errors", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/util", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/version", "")
case "service":
manager.AddStandardImport("context", "")
manager.AddStandardImport("fmt", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/errors", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/filtering", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/util", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/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"
"github.com/PaloAltoNetworks/pango/errors"
"github.com/PaloAltoNetworks/pango/util"
"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

0 comments on commit ba3131c

Please sign in to comment.