Skip to content

Commit

Permalink
Add string method to query types (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanG100 authored Feb 3, 2023
1 parent 9313290 commit 2d4701c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions ygnmi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package ygnmi

import (
"fmt"
"reflect"

"github.com/openconfig/ygot/ygot"
Expand Down Expand Up @@ -141,6 +142,19 @@ type leafBaseQuery[T any] struct {
scalar bool
}

// String returns gNMI path as string for the query.
func (lq *leafBaseQuery[T]) String() string {
protoPath, _, err := ResolvePath(lq.ps)
if err != nil {
return fmt.Sprintf("invalid path: %v", err)
}
path, err := ygot.PathToString(protoPath)
if err != nil {
path = protoPath.String()
}
return path
}

// extract takes the parent GoStruct and returns the correct child field from it.
func (lq *leafBaseQuery[T]) extract(gs ygot.ValidatedGoStruct) (T, bool) {
return lq.extractFn(gs)
Expand Down Expand Up @@ -216,6 +230,19 @@ type nonLeafBaseQuery[T ygot.ValidatedGoStruct] struct {
yschemaFn func() *ytypes.Schema
}

// String returns gNMI path as string for the query.
func (lq *nonLeafBaseQuery[T]) String() string {
protoPath, _, err := ResolvePath(lq.ps)
if err != nil {
return fmt.Sprintf("invalid path: %v", err)
}
path, err := ygot.PathToString(protoPath)
if err != nil {
path = protoPath.String()
}
return path
}

// extract casts the input GoStruct to the concrete type for the query.
// As non-leaves structs are always GoStructs, a simple cast is sufficient.
func (lq *nonLeafBaseQuery[T]) extract(gs ygot.ValidatedGoStruct) (T, bool) {
Expand Down

0 comments on commit 2d4701c

Please sign in to comment.