Skip to content
Snippets Groups Projects

Draft: Resolve "A SetRequest to change a specific path of an OND only works for paths with string values"

Files
2
+ 75
0
 
package util
 
 
import (
 
"fmt"
 
"strconv"
 
 
"github.com/openconfig/gnmi/proto/gnmi"
 
gnmiv "github.com/openconfig/gnmi/value"
 
"github.com/openconfig/goyang/pkg/yang"
 
)
 
 
// ConvertStringToTypedValue takes a value as string and a YangType. The
 
// returned value is the converted TypedValue of the given string based on the
 
// given YangType.
 
func ConvertStringToTypedValue(s string, t *yang.YangType) (*gnmi.TypedValue, error) {
 
// TODO: add more types
 
switch t.Kind {
 
case yang.Yint8:
 
return convertStringToIntTypedValue(s, 10, 8)
 
case yang.Yint16:
 
return convertStringToIntTypedValue(s, 10, 16)
 
case yang.Yint32:
 
return convertStringToIntTypedValue(s, 10, 32)
 
case yang.Yuint8:
 
return convertStringToUintTypedValue(s, 10, 8)
 
case yang.Yuint16:
 
return convertStringToUintTypedValue(s, 10, 16)
 
case yang.Yuint32:
 
return convertStringToUintTypedValue(s, 10, 32)
 
case yang.Ybool:
 
parsedBool, err := strconv.ParseBool(s)
 
if err != nil {
 
return nil, err
 
}
 
return gnmiv.FromScalar(parsedBool)
 
case yang.Ystring:
 
return gnmiv.FromScalar(s)
 
default:
 
return nil, fmt.Errorf("could not convert to TypedValue, unsupported type of: %v", t)
 
}
 
}
 
 
func convertStringToIntTypedValue(s string, base int, bitSize int) (*gnmi.TypedValue, error) {
 
parsedInt, err := strconv.ParseInt(s, base, bitSize)
 
if err != nil {
 
return nil, err
 
}
 
switch bitSize {
 
case 8:
 
return gnmiv.FromScalar(int8(parsedInt))
 
case 16:
 
return gnmiv.FromScalar(int16(parsedInt))
 
case 32:
 
return gnmiv.FromScalar(int32(parsedInt))
 
default:
 
return nil, fmt.Errorf("could not convert string %s to a gnmi.TypedValue (int), unsupported bitSize of: %d", s, bitSize)
 
}
 
}
 
 
func convertStringToUintTypedValue(s string, base int, bitSize int) (*gnmi.TypedValue, error) {
 
parsedInt, err := strconv.ParseUint(s, base, bitSize)
 
if err != nil {
 
return nil, err
 
}
 
switch bitSize {
 
case 8:
 
return gnmiv.FromScalar(uint8(parsedInt))
 
case 16:
 
return gnmiv.FromScalar(uint16(parsedInt))
 
case 32:
 
return gnmiv.FromScalar(uint32(parsedInt))
 
default:
 
return nil, fmt.Errorf("could not convert string %s to a gnmi.TypedValue (uint), unsupported bitSize of: %d", s, bitSize)
 
}
 
}
Loading