Skip to content
Snippets Groups Projects
Commit b51b3542 authored by Malte Bauch's avatar Malte Bauch
Browse files

commented code

parent e0cd9570
No related branches found
No related tags found
1 merge request!213Resolve "Specific paths cant be requested, because the underlying device goStruct can't be filled."
Pipeline #86657 passed
This commit is part of merge request !213. Comments created here will be created in the context of that merge request.
......@@ -82,8 +82,7 @@ func (oc *OpenConfig) Unmarshal(bytes []byte, fields []*path.GnmiPathPiece, goSt
return unmarshal(oc.Schema(), bytes, fields, goStruct, opt...)
}
// unmarshal parses gNMI response to a go struct. If it's a root level response
// it uses
//unmarshal parses a gNMI response to a go struct.
func unmarshal(schema *ytypes.Schema, bytes []byte, fields []*path.GnmiPathPiece, goStruct ygot.ValidatedGoStruct, opt ...ytypes.UnmarshalOpt) error {
defer func() {
if r := recover(); r != nil {
......@@ -160,7 +159,10 @@ func getField(inStruct ygot.ValidatedGoStruct, fields []*path.GnmiPathPiece) (yg
return outStruct, nil
}
// initialiseMapWithKey
// initialiseMapWithKey takes the map type as reflect.Type (expects it to be a
// map[T]*L, where T and L can be everything) and the value that should be
// initialised as reflect.Value. A new map is created and a new key-value pair
// is set, the newly created value is initialised and returned
func initialiseMapWithKey(mType reflect.Type, mValue reflect.Value, gpp *path.GnmiPathPiece) (ygot.ValidatedGoStruct, error) {
defer func() {
if r := recover(); r != nil {
......@@ -169,26 +171,31 @@ func initialiseMapWithKey(mType reflect.Type, mValue reflect.Value, gpp *path.Gn
}()
newMap := reflect.MakeMap(mType)
mValue.Set(newMap)
// This should only contain one entry. since its a map[T]T the type and
// value of the key are not known - therefore it is necessary to loop over
// This should only contain one entry. Since its a map[string]string and the
// key is not known it is necessary to loop over the map to get the entry
for _, k := range gpp.Keys {
kConv, err := convertToType(k, mType.Key().Kind())
ck, err := convertToType(k, mType.Key().Kind())
if err != nil {
return nil, err
}
mValue.SetMapIndex(reflect.ValueOf(kConv), reflect.New(mType.Elem().Elem()))
h := mValue.MapIndex(reflect.ValueOf(kConv)).Interface()
// Add a new key-value pair with the key from gpp.Keys and a pointer to
// a new zero value of type mType.Elem().Elem(), since the map was created
// from mType this is the expected ValueType
mValue.SetMapIndex(reflect.ValueOf(ck), reflect.New(mType.Elem().Elem()))
h := mValue.MapIndex(reflect.ValueOf(ck)).Interface()
outStruct, ok := h.(ygot.ValidatedGoStruct)
if !ok {
return nil, fmt.Errorf("expected ValidatedGoStruct got %v", h)
}
//Initialise the yang tree of the newly created GoStruct
ygot.BuildEmptyTree(outStruct)
return outStruct, nil
}
return nil, fmt.Errorf("could not initialise the map with a key, length of given keys is: %d", len(gpp.Keys))
}
// TODO: refactor: move somewhere else and check which other types are needed
// convertToType allows to convert a string to a specific type, which is
// provided as reflect.Kind
func convertToType(tc string, k reflect.Kind) (interface{}, error) {
switch k {
case reflect.String:
......
......@@ -6,6 +6,7 @@ import (
gpb "github.com/openconfig/gnmi/proto/gnmi"
)
// GnmiPathPiece represents a piece of a gnmi.Path and holds its Name and Keys
type GnmiPathPiece struct {
Name string
Keys map[string]string
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment