Skip to content

Commit

Permalink
Merge pull request #140 from openconfig/improve-style
Browse files Browse the repository at this point in the history
Improve style of context.go
  • Loading branch information
wenovus authored Dec 6, 2023
2 parents e5dde15 + 653a7f1 commit c4957ab
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 96 deletions.
35 changes: 13 additions & 22 deletions ygnmi/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,26 @@ import (

// RequestValues contains request-scoped values for ygnmi queries.
type RequestValues struct {
// CompressedConfigQuery is a key type that means that the query is
// uninterested in /state paths.
CompressedConfigQuery bool
// CompressedStateQuery is a key type that means that the query is
// uninterested in /config paths.
CompressedStateQuery bool
// StateFiltered is a key type that means that the query is
// uninterested in /state paths and will filter them out.
StateFiltered bool
// ConfigFiltered is a key type that means that the query is
// uninterested in /config paths and will filter them out.
ConfigFiltered bool
}

// FromContext extracts certain ygnmi request-scoped values, if present.
func FromContext(ctx context.Context) *RequestValues {
compConfig, _ := ctx.Value(compressedConfigQuery{}).(bool)
compState, _ := ctx.Value(compressedStateQuery{}).(bool)
return &RequestValues{
CompressedConfigQuery: compConfig,
CompressedStateQuery: compState,
}
requestValues, _ := ctx.Value(requestValuesKey{}).(*RequestValues)
return requestValues
}

// NewContext returns a new Context carrying ygnmi request-scoped values.
func NewContext(ctx context.Context, q UntypedQuery) context.Context {
if q.isCompressedSchema() {
if q.IsState() {
return context.WithValue(ctx, compressedStateQuery{}, true)
} else {
return context.WithValue(ctx, compressedConfigQuery{}, true)
}
}
return ctx
return context.WithValue(ctx, requestValuesKey{}, &RequestValues{
StateFiltered: q.isCompressedSchema() && !q.IsState(),
ConfigFiltered: q.isCompressedSchema() && q.IsState(),
})
}

type compressedConfigQuery struct{}
type compressedStateQuery struct{}
type requestValuesKey struct{}
28 changes: 14 additions & 14 deletions ygnmi/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,50 @@ func TestFromContext(t *testing.T) {
desc: "compress-config",
inContext: ygnmi.NewContext(context.Background(), exampleocpath.Root().Parent().Config()),
wantRequestValues: &ygnmi.RequestValues{
CompressedConfigQuery: true,
CompressedStateQuery: false,
StateFiltered: true,
ConfigFiltered: false,
},
}, {
desc: "compress-config-leaf",
inContext: ygnmi.NewContext(context.Background(), exampleocpath.Root().Parent().Child().Five().Config()),
wantRequestValues: &ygnmi.RequestValues{
CompressedConfigQuery: true,
CompressedStateQuery: false,
StateFiltered: true,
ConfigFiltered: false,
},
}, {
desc: "compress-state",
inContext: ygnmi.NewContext(context.Background(), exampleocpath.Root().Parent().State()),
wantRequestValues: &ygnmi.RequestValues{
CompressedConfigQuery: false,
CompressedStateQuery: true,
StateFiltered: false,
ConfigFiltered: true,
},
}, {
desc: "compress-state-leaf",
inContext: ygnmi.NewContext(context.Background(), exampleocpath.Root().Parent().Child().Five().State()),
wantRequestValues: &ygnmi.RequestValues{
CompressedConfigQuery: false,
CompressedStateQuery: true,
StateFiltered: false,
ConfigFiltered: true,
},
}, {
desc: "uncompressed-container",
inContext: ygnmi.NewContext(context.Background(), uexampleocpath.Root().Parent()),
wantRequestValues: &ygnmi.RequestValues{
CompressedConfigQuery: false,
CompressedStateQuery: false,
StateFiltered: false,
ConfigFiltered: false,
},
}, {
desc: "uncompressed-config-container",
inContext: ygnmi.NewContext(context.Background(), uexampleocpath.Root().Parent().Child().Config()),
wantRequestValues: &ygnmi.RequestValues{
CompressedConfigQuery: false,
CompressedStateQuery: false,
StateFiltered: false,
ConfigFiltered: false,
},
}, {
desc: "uncompressed-leaf",
inContext: ygnmi.NewContext(context.Background(), uexampleocpath.Root().Parent().Child().Config().Five()),
wantRequestValues: &ygnmi.RequestValues{
CompressedConfigQuery: false,
CompressedStateQuery: false,
StateFiltered: false,
ConfigFiltered: false,
},
}}

Expand Down
60 changes: 30 additions & 30 deletions ygnmi/ygnmi_preferconfig_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c4957ab

Please sign in to comment.