Skip to content

Commit

Permalink
Merge pull request #21 from ythadhani/nokia-master
Browse files Browse the repository at this point in the history
Allow setting YEmpty type and minor update to processing extensions.
  • Loading branch information
ythadhani authored Nov 9, 2022
2 parents 098c5f0 + 4d42f01 commit d79e2a8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
7 changes: 2 additions & 5 deletions yext/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type ExtensionParams struct {
Keyword, Argument string
}

func ProcessExtensions(value interface{}, extensions string, extHandler ExtensionHandler) (interface{}, error) {
func GetSupportedExtensions(extensions string, extHandler ExtensionHandler) []ExtensionParams {
extSlice := strings.Split(extensions, ";")
var extensionList []ExtensionParams = []ExtensionParams{}
for _, ext := range extSlice {
Expand All @@ -27,8 +27,5 @@ func ProcessExtensions(value interface{}, extensions string, extHandler Extensio
extensionList = append(extensionList, extension)
}
}
if len(extensionList) > 0 {
return extHandler.Process(extensionList, value)
}
return value, nil
return extensionList
}
11 changes: 7 additions & 4 deletions ygot/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,10 +1184,13 @@ func structJSON(s GoStruct, parentMod string, args jsonOutputConfig) (map[string
}

if extensions, hasExtensions := fType.Tag.Lookup("extensions"); hasExtensions && args.extHandler != nil {
value, err = yext.ProcessExtensions(value, extensions, args.extHandler)
if err != nil {
errs.Add(err)
continue
supportedExtensions := yext.GetSupportedExtensions(extensions, args.extHandler)
if len(supportedExtensions) > 0 {
value, err = args.extHandler.Process(supportedExtensions, value)
if err != nil {
errs.Add(fmt.Errorf("failed to process YANG extensions: %s, error: %s", supportedExtensions, err.Error()))
continue
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions ytypes/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,12 @@ func unmarshalStruct(schema *yang.Entry, parent interface{}, jsonTree map[string
return err
}

if extensions, hasExtensions := ft.Tag.Lookup("extensions"); hasExtensions {
if unmarshalConf.extHandler != nil {
jsonValue, err = yext.ProcessExtensions(jsonValue, extensions, unmarshalConf.extHandler)
if extensions, hasExtensions := ft.Tag.Lookup("extensions"); hasExtensions && unmarshalConf.extHandler != nil {
supportedExtensions := yext.GetSupportedExtensions(extensions, unmarshalConf.extHandler)
if len(supportedExtensions) > 0 {
jsonValue, err = unmarshalConf.extHandler.Process(supportedExtensions, jsonValue)
if err != nil {
return err
return fmt.Errorf("failed to process YANG extensions: %s, error: %s", supportedExtensions, err.Error())
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion ytypes/leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ func sanitizeGNMI(parent interface{}, schema *yang.Entry, fieldName string, tv *
}

switch ykind {
case yang.Ybool:
case yang.Ybool, yang.Yempty:
return tv.GetBoolVal(), nil
case yang.Ystring:
return tv.GetStringVal(), nil
Expand Down Expand Up @@ -833,6 +833,11 @@ func gNMIToYANGTypeMatches(ykind yang.TypeKind, tv *gpb.TypedValue, jsonToleranc
switch ykind {
case yang.Ybool:
_, ok = tv.GetValue().(*gpb.TypedValue_BoolVal)
case yang.Yempty:
b, isBoolVal := tv.GetValue().(*gpb.TypedValue_BoolVal)
if isBoolVal {
ok = b.BoolVal
}
case yang.Ystring, yang.Yenum, yang.Yidentityref:
_, ok = tv.GetValue().(*gpb.TypedValue_StringVal)
case yang.Yint8, yang.Yint16, yang.Yint32, yang.Yint64:
Expand Down

0 comments on commit d79e2a8

Please sign in to comment.