@@ -55,21 +55,33 @@ type Capabilities struct {
5555 SupportedModels []Model
5656}
5757
58+ type Client interface {
59+ GetState (ctx context.Context , conf ... Configurable ) error
60+ GetConfig (ctx context.Context , conf ... Configurable ) error
61+ Patch (ctx context.Context , conf ... Configurable ) error
62+ Update (ctx context.Context , conf ... Configurable ) error
63+ Delete (ctx context.Context , conf ... Configurable ) error
64+ }
65+
5866// Client is a gNMI client offering convenience methods for device configuration
5967// using gNMI.
60- type Client struct {
68+ type ClientObj struct {
6169 gnmi gpb.GNMIClient
6270 encoding gpb.Encoding
6371 capabilities * Capabilities
6472 logger logr.Logger
6573}
6674
75+ var (
76+ _ Client = & ClientObj {}
77+ )
78+
6779// New creates a new Client by negotiating capabilities with the gNMI server by
6880// carrying out a Capabilities RPC.
6981// Returns an error if the device doesn't support JSON encoding.
7082// By default, the client uses [slog.Default] for logging.
7183// Use [WithLogger] to provide a custom logger.
72- func New (ctx context.Context , conn grpc.ClientConnInterface , opts ... Option ) (* Client , error ) {
84+ func New (ctx context.Context , conn grpc.ClientConnInterface , opts ... Option ) (Client , error ) {
7385 gnmi := gpb .NewGNMIClient (conn )
7486 res , err := gnmi .Capabilities (ctx , & gpb.CapabilityRequest {})
7587 if err != nil {
@@ -96,18 +108,18 @@ func New(ctx context.Context, conn grpc.ClientConnInterface, opts ...Option) (*C
96108 }
97109 }
98110 logger := logr .FromSlogHandler (slog .Default ().Handler ())
99- client := & Client {gnmi , encoding , capabilities , logger }
111+ client := & ClientObj {gnmi , encoding , capabilities , logger }
100112 for _ , opt := range opts {
101113 opt (client )
102114 }
103115 return client , nil
104116}
105117
106- type Option func (* Client )
118+ type Option func (* ClientObj )
107119
108120// WithLogger sets a custom logger for the client.
109121func WithLogger (logger logr.Logger ) Option {
110- return func (c * Client ) {
122+ return func (c * ClientObj ) {
111123 c .logger = logger
112124 }
113125}
@@ -117,34 +129,34 @@ var ErrNil = errors.New("gnmiext: nil")
117129
118130// GetConfig retrieves config and unmarshals it into the provided targets.
119131// If some of the values for the given xpaths are not defined, [ErrNil] is returned.
120- func (c * Client ) GetConfig (ctx context.Context , conf ... Configurable ) error {
132+ func (c * ClientObj ) GetConfig (ctx context.Context , conf ... Configurable ) error {
121133 return c .get (ctx , gpb .GetRequest_CONFIG , conf ... )
122134}
123135
124136// GetState retrieves state and unmarshals it into the provided targets.
125137// If some of the values for the given xpaths are not defined, [ErrNil] is returned.
126- func (c * Client ) GetState (ctx context.Context , conf ... Configurable ) error {
138+ func (c * ClientObj ) GetState (ctx context.Context , conf ... Configurable ) error {
127139 return c .get (ctx , gpb .GetRequest_STATE , conf ... )
128140}
129141
130142// Update replaces the configuration for the given set of items.
131143// If the current configuration equals the desired configuration, the operation is skipped.
132144// For partial updates that merge changes, use [Client.Patch] instead.
133- func (c * Client ) Update (ctx context.Context , conf ... Configurable ) error {
145+ func (c * ClientObj ) Update (ctx context.Context , conf ... Configurable ) error {
134146 return c .set (ctx , false , conf ... )
135147}
136148
137149// Patch merges the configuration for the given set of items.
138150// If the current configuration equals the desired configuration, the operation is skipped.
139151// For full replacement of configuration, use [Client.Update] instead.
140- func (c * Client ) Patch (ctx context.Context , conf ... Configurable ) error {
152+ func (c * ClientObj ) Patch (ctx context.Context , conf ... Configurable ) error {
141153 return c .set (ctx , true , conf ... )
142154}
143155
144156// Delete resets the configuration for the given set of items.
145157// If an item implements [Defaultable], it's reset to default value.
146158// Otherwise, the configuration is deleted.
147- func (c * Client ) Delete (ctx context.Context , conf ... Configurable ) error {
159+ func (c * ClientObj ) Delete (ctx context.Context , conf ... Configurable ) error {
148160 if len (conf ) == 0 {
149161 return nil
150162 }
@@ -179,7 +191,7 @@ func (c *Client) Delete(ctx context.Context, conf ...Configurable) error {
179191// get retrieves data of the specified type (CONFIG or STATE) and unmarshals it
180192// into the provided targets. If some of the values for the given xpaths are not
181193// defined, [ErrNil] is returned.
182- func (c * Client ) get (ctx context.Context , dt gpb.GetRequest_DataType , conf ... Configurable ) error {
194+ func (c * ClientObj ) get (ctx context.Context , dt gpb.GetRequest_DataType , conf ... Configurable ) error {
183195 if len (conf ) == 0 {
184196 return nil
185197 }
@@ -244,7 +256,7 @@ func (c *Client) get(ctx context.Context, dt gpb.GetRequest_DataType, conf ...Co
244256// configuration. Otherwise, a full replacement is done.
245257// If the current configuration equals the desired configuration, the operation
246258// is skipped.
247- func (c * Client ) set (ctx context.Context , patch bool , conf ... Configurable ) error {
259+ func (c * ClientObj ) set (ctx context.Context , patch bool , conf ... Configurable ) error {
248260 if len (conf ) == 0 {
249261 return nil
250262 }
@@ -293,7 +305,7 @@ func (c *Client) set(ctx context.Context, patch bool, conf ...Configurable) erro
293305// Marshal marshals the provided value into a byte slice using the client's encoding.
294306// If the value implements the [Marshaler] interface, it will be marshaled using that.
295307// Otherwise, [json.Marshal] is used.
296- func (c * Client ) Marshal (v any ) (b []byte , err error ) {
308+ func (c * ClientObj ) Marshal (v any ) (b []byte , err error ) {
297309 if m , ok := v .(Marshaler ); ok {
298310 b , err = m .MarshalYANG (c .capabilities )
299311 if err != nil {
@@ -311,7 +323,7 @@ func (c *Client) Marshal(v any) (b []byte, err error) {
311323// Unmarshal unmarshals the provided byte slice into the provided destination.
312324// If the destination implements the [Marshaler] interface, it will be unmarshaled using that.
313325// Otherwise, [json.Unmarshal] is used.
314- func (c * Client ) Unmarshal (b []byte , dst any ) (err error ) {
326+ func (c * ClientObj ) Unmarshal (b []byte , dst any ) (err error ) {
315327 // NOTE: If you query for list elements on Cisco NX-OS, the encoded payload
316328 // will be the wrapped in an array (even if only one element is requested), i.e.
317329 //
@@ -339,7 +351,7 @@ func (c *Client) Unmarshal(b []byte, dst any) (err error) {
339351}
340352
341353// Encode encodes the provided byte slice into a [gpb.TypedValue] using the client's encoding.
342- func (c * Client ) Encode (b []byte ) * gpb.TypedValue {
354+ func (c * ClientObj ) Encode (b []byte ) * gpb.TypedValue {
343355 switch c .encoding {
344356 case gpb .Encoding_JSON :
345357 return & gpb.TypedValue {
@@ -359,7 +371,7 @@ func (c *Client) Encode(b []byte) *gpb.TypedValue {
359371}
360372
361373// Decode decodes the provided [gpb.TypedValue] into the provided destination using the client's encoding.
362- func (c * Client ) Decode (val * gpb.TypedValue ) ([]byte , error ) {
374+ func (c * ClientObj ) Decode (val * gpb.TypedValue ) ([]byte , error ) {
363375 switch c .encoding {
364376 case gpb .Encoding_JSON :
365377 v , ok := val .Value .(* gpb.TypedValue_JsonVal )
0 commit comments