openconfig / ygot

A YANG-centric Go toolkit - Go/Protobuf Code Generation; Validation; Marshaling/Unmarshaling
Apache License 2.0
286 stars 107 forks source link

ygotutils.GetNode strips the module name by default #326

Closed soumiksamanta closed 5 years ago

soumiksamanta commented 5 years ago

ygot@v0.6.0

I am trying to create a golang app on similar grounds as provided in https://github.com/google/gnxi/blob/master/gnmi/server_test.go

In all the gNMI SET test scenarios in the above test file, the setRequest uses path without the moduleName prepended. I want to use the following Path to do a gnmi SET txtPbPath := elem: <name: "openconfig-platform:component" >

If I use the above path which includes modulename, I get error from ygot "could not find path in tree beyond type model.Device, remaining path openconfig-platform:component"

The reason for this is the following code snippet from https://github.com/openconfig/ygot/blob/master/experimental/ygotutils/common.go

// schemaPaths returns all the paths in the path tag, plus the path value of
// the rootname tag, if one is present.
func schemaPaths(f reflect.StructField) ([][]string, error) {
    var out [][]string
    rootTag, ok := f.Tag.Lookup("rootname")
    if ok {
        out = append(out, strings.Split(rootTag, "/"))
    }
    pathTag, ok := f.Tag.Lookup("path")
    if (!ok || pathTag == "") && rootTag == "" {
        return nil, fmt.Errorf("field %s did not specify a path", f.Name)
    }
    if pathTag == "" {
        return out, nil
    }

    ps := strings.Split(pathTag, "|")
    for _, p := range ps {
        sp := removeRootPrefix(strings.Split(p, "/"))
        out = append(out, stripModulePrefixes(sp))
    }
    return out, nil
}

The method only looks at the path tag from the generated GoStruct Is there a knob, where the module tag is also included and its not stripped off?

wenovus commented 5 years ago

Hi, no there isn't a knob as the current code also operates on paths without the module prefixes since there are no conflicts.

Is it ok for you to strip the module prefixes? In the current code there are the helpers StripModulePrefix and StripModulePrefixesStr which you can make use of.

soumiksamanta commented 5 years ago

Thanks! I strip the modulename from the path elements. But I still feel that it would be great if the ygot library does it internally for me. Similar to what today EmitJSON does, ex. pass the option of AppendModuleName, disable Validation etc.