Skip to content

Commit

Permalink
Fix some style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
wenovus committed Mar 7, 2020
1 parent f9291df commit 3eef40c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
1 change: 1 addition & 0 deletions ygot/path_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
42 changes: 23 additions & 19 deletions ypathgen/pathgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
6 changes: 3 additions & 3 deletions ypathgen/pathgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1868,22 +1868,22 @@ 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",
inUniqueFieldName: "List",
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",
Expand Down

0 comments on commit 3eef40c

Please sign in to comment.