Skip to content

Commit ae3ccb7

Browse files
committed
Rewrite SDK codegen for structures, normalizers and specifiers
1 parent eafa45b commit ae3ccb7

15 files changed

+1505
-1292
lines changed

assets/pango/example/main.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
"encoding/xml"
65
"fmt"
76
"log"
87

@@ -1038,21 +1037,6 @@ func checkService(c *pango.Client, ctx context.Context) {
10381037
return
10391038
}
10401039

1041-
readDescription := ""
1042-
if serviceReply.Description != nil {
1043-
readDescription = *serviceReply.Description
1044-
}
1045-
1046-
keys := make([]string, 0, len(serviceReply.Misc))
1047-
xmls := make([]string, 0, len(serviceReply.Misc))
1048-
for key := range serviceReply.Misc {
1049-
keys = append(keys, key)
1050-
data, _ := xml.Marshal(serviceReply.Misc[key])
1051-
xmls = append(xmls, string(data))
1052-
}
1053-
log.Printf("Service '%s=%s, description: %s misc XML: %s, misc keys: %s' read",
1054-
serviceReply.Name, *serviceReply.Protocol.Tcp.Port, readDescription, xmls, keys)
1055-
10561040
// SERVICE - UPDATE 3
10571041
serviceReply.Description = util.String("some text changed now")
10581042

@@ -1061,21 +1045,6 @@ func checkService(c *pango.Client, ctx context.Context) {
10611045
log.Printf("Failed to update object: %s", err)
10621046
return
10631047
}
1064-
1065-
readDescription = ""
1066-
if serviceReply.Description != nil {
1067-
readDescription = *serviceReply.Description
1068-
}
1069-
1070-
keys = make([]string, 0, len(serviceReply.Misc))
1071-
xmls = make([]string, 0, len(serviceReply.Misc))
1072-
for key := range serviceReply.Misc {
1073-
keys = append(keys, key)
1074-
data, _ := xml.Marshal(serviceReply.Misc[key])
1075-
xmls = append(xmls, string(data))
1076-
}
1077-
log.Printf("Service '%s=%s, description: %s misc XML: %s, misc keys: %s' update",
1078-
serviceReply.Name, *serviceReply.Protocol.Tcp.Port, readDescription, xmls, keys)
10791048
}
10801049

10811050
func checkNtp(c *pango.Client, ctx context.Context) {

pkg/generate/generator.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,9 @@ func (c *Creator) parseTemplate(templateName string) (*template.Template, error)
256256
"packageName": translate.PackageName,
257257
"locationType": translate.LocationType,
258258
"specParamType": translate.SpecParamType,
259-
"xmlParamType": translate.XmlParamType,
260259
"xmlName": translate.XmlName,
260+
"xmlParamType": translate.XmlParamType,
261261
"xmlTag": translate.XmlTag,
262-
"specifyEntryAssignment": translate.SpecifyEntryAssignmentTmpl,
263-
"normalizeAssignment": translate.NormalizeAssignmentTmpl,
264262
"specMatchesFunction": translate.SpecMatchesFunction,
265263
"nestedSpecMatchesFunction": translate.NestedSpecMatchesFunction,
266264
"omitEmpty": translate.OmitEmpty,
@@ -272,10 +270,24 @@ func (c *Creator) parseTemplate(templateName string) (*template.Template, error)
272270
return a - b
273271
},
274272
"generateEntryXpath": translate.GenerateEntryXpath,
275-
"nestedSpecs": translate.NestedSpecs,
276-
"RenderEntryXmlStructs": func(spec *properties.Normalization) (string, error) {
273+
"RenderApiStructs": func(spec *properties.Normalization) (string, error) {
274+
return translate.RenderEntryApiStructs(spec)
275+
},
276+
"RenderXmlStructs": func(spec *properties.Normalization) (string, error) {
277277
return translate.RenderEntryXmlStructs(spec)
278278
},
279+
"RenderXmlContainerNormalizers": func(spec *properties.Normalization) (string, error) {
280+
return translate.RenderXmlContainerNormalizers(spec)
281+
},
282+
"RenderXmlContainerSpecifiers": func(spec *properties.Normalization) (string, error) {
283+
return translate.RenderXmlContainerSpecifiers(spec)
284+
},
285+
"RenderToXmlMarshallers": func(spec *properties.Normalization) (string, error) {
286+
return translate.RenderToXmlMarshalers(spec)
287+
},
288+
"RenderSpecMatchers": func(spec *properties.Normalization) (string, error) {
289+
return translate.RenderSpecMatchers(spec)
290+
},
279291
"createGoSuffixFromVersion": translate.CreateGoSuffixFromVersionTmpl,
280292
"paramSupportedInVersion": translate.ParamSupportedInVersionTmpl,
281293
"xmlPathSuffixes": translate.XmlPathSuffixes,

pkg/properties/namevariant.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package properties
2+
3+
import (
4+
"strings"
5+
6+
"github.com/paloaltonetworks/pan-os-codegen/pkg/naming"
7+
)
8+
9+
type NameVariant struct {
10+
Original string
11+
Underscore string
12+
CamelCase string
13+
LowerCamelCase string
14+
}
15+
16+
func NewNameVariant(name string) *NameVariant {
17+
return &NameVariant{
18+
Original: name,
19+
Underscore: naming.Underscore("", name, ""),
20+
CamelCase: naming.CamelCase("", name, "", true),
21+
LowerCamelCase: naming.CamelCase("", name, "", false),
22+
}
23+
}
24+
25+
func (o NameVariant) Components() []string {
26+
return strings.Split(o.Original, "-")
27+
}
28+
29+
func (o NameVariant) IsEmpty() bool {
30+
return o.Original == ""
31+
}
32+
33+
func (o NameVariant) WithSuffix(suffix *NameVariant) *NameVariant {
34+
if o.Original == "" {
35+
return NewNameVariant(suffix.Original)
36+
} else {
37+
return NewNameVariant(o.Original + "-" + suffix.Original)
38+
}
39+
}
40+
41+
func (o NameVariant) WithLiteralSuffix(suffix string) *NameVariant {
42+
return &NameVariant{
43+
Original: o.Original + suffix,
44+
CamelCase: o.CamelCase + suffix,
45+
LowerCamelCase: o.LowerCamelCase + suffix,
46+
Underscore: o.Underscore + suffix,
47+
}
48+
}

pkg/properties/namevariant_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package properties_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
7+
"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
8+
)
9+
10+
var _ = Describe("NameVariant", func() {
11+
Context("When creating name variant from empty string", func() {
12+
It("should return empty string for all variants", func() {
13+
variant := properties.NewNameVariant("")
14+
Expect(variant).To(Equal(&properties.NameVariant{
15+
Original: "",
16+
LowerCamelCase: "",
17+
CamelCase: "",
18+
Underscore: "",
19+
}))
20+
})
21+
})
22+
})

pkg/properties/normalized.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"strings"
1212

1313
"github.com/paloaltonetworks/pan-os-codegen/pkg/content"
14-
"github.com/paloaltonetworks/pan-os-codegen/pkg/naming"
1514
"github.com/paloaltonetworks/pan-os-codegen/pkg/schema/object"
1615
"github.com/paloaltonetworks/pan-os-codegen/pkg/schema/parameter"
1716
"github.com/paloaltonetworks/pan-os-codegen/pkg/schema/validator"
@@ -85,26 +84,6 @@ type TerraformProviderConfig struct {
8584
PluralDescription string `json:"plural_description" yaml:"plural_description"`
8685
}
8786

88-
type NameVariant struct {
89-
Original string
90-
Underscore string
91-
CamelCase string
92-
LowerCamelCase string
93-
}
94-
95-
func NewNameVariant(name string) *NameVariant {
96-
return &NameVariant{
97-
Original: name,
98-
Underscore: naming.Underscore("", name, ""),
99-
CamelCase: naming.CamelCase("", name, "", true),
100-
LowerCamelCase: naming.CamelCase("", name, "", false),
101-
}
102-
}
103-
104-
func (o *NameVariant) Components() []string {
105-
return strings.Split(o.Original, "-")
106-
}
107-
10887
type Location struct {
10988
Name *NameVariant
11089
SpecOrder int
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package properties_test
2+
3+
import (
4+
"log/slog"
5+
"testing"
6+
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
)
10+
11+
func TestMovement(t *testing.T) {
12+
handler := slog.NewTextHandler(GinkgoWriter, &slog.HandlerOptions{
13+
Level: slog.LevelDebug,
14+
})
15+
slog.SetDefault(slog.New(handler))
16+
RegisterFailHandler(Fail)
17+
RunSpecs(t, "Properties Suite")
18+
}

0 commit comments

Comments
 (0)