Skip to content

Commit

Permalink
Merge pull request #107 from openconfig/remove-unified-templates
Browse files Browse the repository at this point in the history
Remove unified pathstruct templates
  • Loading branch information
wenovus authored Jul 17, 2023
2 parents d9ec36c + e4fbcd9 commit e57b695
Showing 1 changed file with 24 additions and 63 deletions.
87 changes: 24 additions & 63 deletions pathgen/pathgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func Root() *{{ .TypeName }} {
// goPathStructTemplate defines the template for the type definition of
// a path node as well as its core method(s). A path struct/node is
// either a container, list, or a leaf node in the openconfig schema
// where the tree formed by the nodes mirrors the compressed YANG
// where the tree formed by the nodes mirrors the YANG
// schema tree. The defined type stores the relative path to the
// current node, as well as its parent node for obtaining its absolute
// path. There are two versions of these, non-wildcard and wildcard.
Expand All @@ -627,33 +627,19 @@ func Root() *{{ .TypeName }} {
// {{ .TypeName }} represents the {{ .YANGPath }} YANG schema element.
type {{ .TypeName }} struct {
*ygnmi.{{ .PathBaseTypeName }}
}
{{- if .GenerateWildcardPaths }}
// {{ .TypeName }}{{ .WildcardSuffix }} represents the wildcard version of the {{ .YANGPath }} YANG schema element.
type {{ .TypeName }}{{ .WildcardSuffix }} struct {
*ygnmi.{{ .PathBaseTypeName }}
}
{{- end }}
`)

// goUnifiedLeafPathStructTemplate is similar to goPathStructTemplate except
// leaves are not PathStructs; instead they have Config and State methods
// that return path structs.
goUnifiedLeafPathStructTemplate = mustTemplate("leaf-struct", `
// {{ .TypeName }} represents the {{ .YANGPath }} YANG schema element.
type {{ .TypeName }} struct {
*ygnmi.{{ .PathBaseTypeName }}
{{- if .GenerateParentField }}
parent ygnmi.PathStruct
{{- end }}
}
{{- if .GenerateWildcardPaths }}
// {{ .TypeName }}{{ .WildcardSuffix }} represents the wildcard version of the {{ .YANGPath }} YANG schema element.
type {{ .TypeName }}{{ .WildcardSuffix }} struct {
*ygnmi.{{ .PathBaseTypeName }}
{{- if .GenerateParentField }}
parent ygnmi.PathStruct
{{- end }}
}
{{- end }}
`)
Expand All @@ -680,28 +666,9 @@ func (n *{{ .Struct.TypeName }}) {{ .MethodName -}} ({{ .KeyParamListStr }}) *{{
map[string]interface{}{ {{- .KeyEntriesStr -}} },
n,
),
}
}
`)

// goUnifiedLeafPathChildConstructorTemplate generates the child constructor method
// for a generated struct by returning an instantiation of the child's
// path struct object. In the unified model, leaves are not path structs
// because with path compression, a leaf path may be a state or config path.
goUnifiedLeafPathChildConstructorTemplate = mustTemplate("unifiedchildConstructor", `
// {{ .MethodName }} ({{ .YANGNodeType }}): {{ .YANGDescription }}
// Defining module: "{{ .DefiningModuleName }}"
// Instantiating module: "{{ .InstantiatingModuleName }}"
// Path from parent: "{{ .RelPath }}"
// Path from root: "{{ .AbsPath }}"
func (n *{{ .Struct.TypeName }}) {{ .MethodName -}} ({{ .KeyParamListStr }}) *{{ .ChildPkgAccessor }}{{ .TypeName }} {
return &{{ .ChildPkgAccessor }}{{ .TypeName }}{
{{ .Struct.PathBaseTypeName }}: ygnmi.New{{ .Struct.PathBaseTypeName }}(
[]string{ {{- .RelPathList -}} },
map[string]interface{}{ {{- .KeyEntriesStr -}} },
n,
),
{{- if .GenerateParentField }}
parent: n,
{{- end }}
}
}
`)
Expand Down Expand Up @@ -934,6 +901,10 @@ type goPathStructData struct {
WildcardSuffix string
// GenerateWildcardPaths means to generate wildcard nodes and paths.
GenerateWildcardPaths bool
// GenerateParentField means to generate a parent PathStruct field in
// the PathStruct so that it's accessible by code within the generated
// file's package.
GenerateParentField bool
}

// getStructData returns the goPathStructData corresponding to a
Expand Down Expand Up @@ -969,6 +940,10 @@ type goPathFieldData struct {
KeyEntriesStr string // KeyEntriesStr is an ordered list of comma-separated ("schemaName": unique camel-case name) for a list's keys.
KeyParamDocStrs []string // KeyParamDocStrs is an ordered slice of docstrings documenting the types of each list key parameter.
ChildPkgAccessor string // ChildPkgAccessor is used if the child path struct exists in another package.
// GenerateParentField means to generate a parent PathStruct field in
// the PathStruct so that it's accessible by code within the generated
// file's package.
GenerateParentField bool
}

func isKeyedList(directory *ygen.ParsedDirectory) bool {
Expand Down Expand Up @@ -1094,15 +1069,10 @@ func generateDirectorySnippet(directory *ygen.ParsedDirectory, directories map[s
PathStructInterfaceName: ygnmi.PathStructInterfaceName,
WildcardSuffix: WildcardSuffix,
GenerateWildcardPaths: generateWildcardPaths,
GenerateParentField: unified,
}
if unified {
if err := goUnifiedLeafPathStructTemplate.Execute(&buf, structData); err != nil {
errs = util.AppendErr(errs, err)
}
} else {
if err := goPathStructTemplate.Execute(&buf, structData); err != nil {
errs = util.AppendErr(errs, err)
}
if err := goPathStructTemplate.Execute(&buf, structData); err != nil {
errs = util.AppendErr(errs, err)
}
}
leafSnippet := GoPathStructCodeSnippet{
Expand Down Expand Up @@ -1259,13 +1229,10 @@ func generateChildConstructorsForLeafOrContainer(methodBuf *strings.Builder, fie
// Generate child constructor for the non-wildcard version of the parent struct.
var errors []error
if unified && isLeaf {
if err := goUnifiedLeafPathChildConstructorTemplate.Execute(methodBuf, fieldData); err != nil {
errors = append(errors, err)
}
} else {
if err := goPathChildConstructorTemplate.Execute(methodBuf, fieldData); err != nil {
errors = append(errors, err)
}
fieldData.GenerateParentField = true
}
if err := goPathChildConstructorTemplate.Execute(methodBuf, fieldData); err != nil {
errors = append(errors, err)
}

// The root node doesn't have a wildcard version of itself.
Expand All @@ -1279,14 +1246,8 @@ func generateChildConstructorsForLeafOrContainer(methodBuf *strings.Builder, fie
fieldData.TypeName += WildcardSuffix
fieldData.Struct.TypeName += WildcardSuffix

if unified && isLeaf {
if err := goUnifiedLeafPathChildConstructorTemplate.Execute(methodBuf, fieldData); err != nil {
errors = append(errors, err)
}
} else {
if err := goPathChildConstructorTemplate.Execute(methodBuf, fieldData); err != nil {
errors = append(errors, err)
}
if err := goPathChildConstructorTemplate.Execute(methodBuf, fieldData); err != nil {
errors = append(errors, err)
}
}

Expand Down

0 comments on commit e57b695

Please sign in to comment.