diff --git a/ygot/path_types.go b/ygot/path_types.go index f4a21685f..52bc9bb6d 100644 --- a/ygot/path_types.go +++ b/ygot/path_types.go @@ -58,6 +58,7 @@ func ResolveRelPath(n PathStruct) ([]*gpb.PathElem, []error) { return n.relPath() } +// ModifyKey can be used to update a key's value. func (n *NodePath) ModifyKey(name string, value interface{}) { n.keys[name] = value } diff --git a/ypathgen/pathgen.go b/ypathgen/pathgen.go index 6843cd85a..29c7397a0 100644 --- a/ypathgen/pathgen.go +++ b/ypathgen/pathgen.go @@ -780,49 +780,53 @@ func generateChildConstructorsForListBuilderFormat(methodBuf *strings.Builder, l } keyN := len(keyParams) + // Initialize ygot.NodePath's key list with wildcard values. var keyEntryStrs []string for i := 0; i != keyN; i++ { keyEntryStrs = append(keyEntryStrs, fmt.Sprintf(`"%s": "*"`, keyParams[i].name)) } - // Create the string for the method parameter list and ygot.NodePath's key list. - fieldData.KeyParamListStr = "" fieldData.KeyEntriesStr = strings.Join(keyEntryStrs, ", ") + // There are no initial key parameters for the builder API. + fieldData.KeyParamListStr = "" + // Set the child type to be the wildcard version. fieldData.TypeName += WildcardSuffix - // Since all keys are wildcarded, just use BuilderCtorSuffix alone as the suffix. + // Add Builder suffix to the child constructor method name. fieldData.MethodName += BuilderCtorSuffix - // Generate child constructor method for non-wildcard version of parent struct. + // Generate builder constructor method for non-wildcard version of parent struct. if err := goPathChildConstructorTemplate.Execute(methodBuf, fieldData); err != nil { errors = append(errors, err) } // The root node doesn't have a wildcard version of itself. if !isUnderFakeRoot { - // Generate child constructor method for wildcard version of parent struct. + // Generate builder constructor method for wildcard version of parent struct. fieldData.Struct.TypeName += WildcardSuffix if err := goPathChildConstructorTemplate.Execute(methodBuf, fieldData); err != nil { return append(errors, err) } } + // Generate key-builder methods for the wildcard version of the PathStruct. + // Although non-wildcard PathStruct is unnecessary, it is kept for generation simplicity. for i := 0; i != keyN; i++ { - builder := struct { - MethodName string - TypeName string - KeySchemaName string - KeyParamType string - KeyParamName string - }{ - MethodName: BuilderKeyPrefix + keyParams[i].varName, - TypeName: fieldData.TypeName, - KeySchemaName: keyParams[i].name, - KeyParamName: keyParams[i].varName, - KeyParamType: keyParams[i].typeName, - } - if err := goKeyBuilderTemplate.Execute(methodBuf, builder); err != nil { + if err := goKeyBuilderTemplate.Execute(methodBuf, + struct { + MethodName string + TypeName string + KeySchemaName string + KeyParamType string + KeyParamName string + }{ + MethodName: BuilderKeyPrefix + keyParams[i].varName, + TypeName: fieldData.TypeName, + KeySchemaName: keyParams[i].name, + KeyParamName: keyParams[i].varName, + KeyParamType: keyParams[i].typeName, + }); err != nil { return append(errors, err) } } diff --git a/ypathgen/pathgen_test.go b/ypathgen/pathgen_test.go index 3e1f8069e..9510b9954 100644 --- a/ypathgen/pathgen_test.go +++ b/ypathgen/pathgen_test.go @@ -1868,14 +1868,14 @@ func (n *Root) ListWithState(Key float64) *ListWithState { } `, }, { - name: "list methods", + name: "root-level list methods", inDirectory: directories["/root"], inDirectories: directories, inFieldName: "list", inUniqueFieldName: "List", want: wantListMethods, }, { - name: "list methods with builder API threshold over the number of keys", + name: "root-level list methods with builder API threshold over the number of keys", inDirectory: directories["/root"], inDirectories: directories, inFieldName: "list", @@ -1883,7 +1883,7 @@ func (n *Root) ListWithState(Key float64) *ListWithState { inListBuilderKeyThreshold: 4, want: wantListMethods, }, { - name: "list methods over key threshold -- should use builder API", + name: "root-level list methods over key threshold -- should use builder API", inDirectory: directories["/root"], inDirectories: directories, inFieldName: "list",