Skip to content

Commit

Permalink
Add OrderedList type to IR (#879)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenovus authored Jun 16, 2023
1 parent 2060d30 commit dfbe14e
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 4 deletions.
7 changes: 7 additions & 0 deletions gogen/ordered_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package gogen

import (
"bytes"
"fmt"
)

// generatedOrderedMapStruct contains the necessary information to generate an
Expand All @@ -41,6 +42,12 @@ type generatedOrderedMapStruct struct {
YANGPath string
}

// OrderedMapTypeName returns the type name of an ordered map given the type
// name of the ordered map list element.
func OrderedMapTypeName(listElemTypeName string) string {
return fmt.Sprintf("%s_OrderedMap", listElemTypeName)
}

var (
goOrderedMapParentMethodsTemplate = mustMakeTemplate("orderedMapParentMethods", `
// AppendNew{{ .ListFieldName }} creates a new entry in the {{ .ListFieldName }}
Expand Down
2 changes: 1 addition & 1 deletion gogen/unordered_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ func yangListFieldToGoType(listField *ygen.NodeDetails, listFieldName string, pa
var orderedMapSpec *generatedOrderedMapStruct

if listField.YANGDetails.OrderedByUser {
structName := fmt.Sprintf("%s_OrderedMap", listElem.Name)
structName := OrderedMapTypeName(listElem.Name)
listType = fmt.Sprintf("*%s", structName)
// Create spec for generating ordered maps.
orderedMapSpec = &generatedOrderedMapStruct{
Expand Down
2 changes: 1 addition & 1 deletion protogen/protogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func outputNestedMessage(msg *ygen.ParsedDirectory, compressPaths bool) bool {
// parent will have been removed, therefore this is a valid message. The path
// is 4 elements long since it is of the form
// []string{"", module-name, surrounding-container, list-name}.
if compressPaths && msg.Type == ygen.List && len(strings.Split(msg.Path, "/")) == 4 {
if compressPaths && (msg.Type == ygen.List || msg.Type == ygen.OrderedList) && len(strings.Split(msg.Path, "/")) == 4 {
return true
}

Expand Down
3 changes: 3 additions & 0 deletions ygen/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ func getOrderedDirDetails(langMapper LangMapper, directory map[string]*Directory
switch {
case dir.Entry.IsList():
pd.Type = List
if dir.Entry.ListAttr.OrderedByUser {
pd.Type = OrderedList
}
if !util.IsUnkeyedList(dir.Entry) {
pd.ListKeys = dir.ListAttr.Keys
pd.ListKeyYANGNames = dir.ListAttr.ListKeyYANGNames
Expand Down
4 changes: 3 additions & 1 deletion ygen/ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,10 @@ const (
_ DirType = iota
// Container represents a YANG 'container'.
Container
// List represents a YANG 'list'.
// List represents a YANG 'list' that is 'ordered-by system'.
List
// OrderedList represents a YANG 'list' that is 'ordered-by user'.
OrderedList
)

// NodeDetails describes an individual field of the generated
Expand Down
2 changes: 1 addition & 1 deletion ygot/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,7 @@ func jsonValue(field reflect.Value, parentMod string, args jsonOutputConfig) (an
case reflect.Struct:
goStruct, ok := field.Interface().(GoStruct)
if !ok {
return nil, fmt.Errorf("cannot map struct %v, invalid GoStruct", field)
return nil, fmt.Errorf("cannot map struct (%T, %v), invalid GoStruct", field.Interface(), field)
}

var err error
Expand Down

0 comments on commit dfbe14e

Please sign in to comment.