Skip to content

Commit

Permalink
Use imports manager
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech committed Apr 3, 2024
1 parent ec3c02e commit 7abbcd2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 24 deletions.
1 change: 1 addition & 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 Down
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")
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
9 changes: 1 addition & 8 deletions templates/sdk/service.tmpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package {{packageName .GoSdkPath}}
{{- if .Entry}}
import (
"context"
"fmt"
{{renderImports "service"}}

"github.com/PaloAltoNetworks/pango/errors"
"github.com/PaloAltoNetworks/pango/filtering"
"github.com/PaloAltoNetworks/pango/util"
"github.com/PaloAltoNetworks/pango/xmlapi"
)

type Service struct {
client util.PangoClient
Expand Down

0 comments on commit 7abbcd2

Please sign in to comment.