Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metadata field to config model #260

Merged
merged 6 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/lekkodev/cli
go 1.20

require (
buf.build/gen/go/lekkodev/cli/bufbuild/connect-go v1.10.0-20230808195657-843d07fa9e34.1
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.31.0-20230808195657-843d07fa9e34.1
buf.build/gen/go/lekkodev/cli/bufbuild/connect-go v1.10.0-20231019182200-bf5993a63215.1
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.31.0-20231019182200-bf5993a63215.1
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/bazelbuild/buildtools v0.0.0-20220907133145-b9bfff5d7f91
github.com/bufbuild/connect-go v1.10.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buf.build/gen/go/lekkodev/cli/bufbuild/connect-go v1.10.0-20230808195657-843d07fa9e34.1 h1:mt4QLKZtQwEBqRm2bi7aj4/PJ8AJQg2Tq0G3hJHzwHc=
buf.build/gen/go/lekkodev/cli/bufbuild/connect-go v1.10.0-20230808195657-843d07fa9e34.1/go.mod h1:15ARpvleKTFJ7xogv/ihJL1cigfDJij4juFMBQzY+Eo=
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.31.0-20230808195657-843d07fa9e34.1 h1:VA17A//BYMH5yjd1ADllRk5p+fmIUQ7mauEZ31zKdDM=
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.31.0-20230808195657-843d07fa9e34.1/go.mod h1:mYcnts9MJhUckfRD5/qOThXC7Kaj7O714LOw5GoGZ9c=
buf.build/gen/go/lekkodev/cli/bufbuild/connect-go v1.10.0-20231019182200-bf5993a63215.1 h1:BwvI+w/S2N1l+UxOypD6B97J9yQOuraDHtmd6p1DVwA=
buf.build/gen/go/lekkodev/cli/bufbuild/connect-go v1.10.0-20231019182200-bf5993a63215.1/go.mod h1:wn4VgRwpFL7yJp8F+7REWc5WtA+7cmZia4n8iV2nzcw=
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.31.0-20231019182200-bf5993a63215.1 h1:8ax4GaVV2IIKJuSCsZqJPEZiLVPOrIQiwip9j45Y+zs=
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.31.0-20231019182200-bf5993a63215.1/go.mod h1:mYcnts9MJhUckfRD5/qOThXC7Kaj7O714LOw5GoGZ9c=
buf.build/gen/go/lekkodev/sdk/protocolbuffers/go v1.31.0-20230419180142-0694c10ef23c.1/go.mod h1:UOnQUnbc9uR4s5SlhBKspO4dffz+T3A6X200yYBnaZg=
buf.build/gen/go/lekkodev/sdk/protocolbuffers/go v1.31.0-20230810202034-1c821065b9a0.1 h1:jd5EUbnTPSEuCL+U3TsK4fjym4eVveXJCOssOUOiJcM=
buf.build/gen/go/lekkodev/sdk/protocolbuffers/go v1.31.0-20230810202034-1c821065b9a0.1/go.mod h1:UOnQUnbc9uR4s5SlhBKspO4dffz+T3A6X200yYBnaZg=
Expand Down
1 change: 1 addition & 0 deletions pkg/feature/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ type Feature struct {
Value interface{}
FeatureType eval.ConfigType
Namespace string
Metadata map[string]any

Overrides []*Override
UnitTests []UnitTest
Expand Down
23 changes: 23 additions & 0 deletions pkg/star/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
ResultVariableName string = "result"
DefaultValueAttrName string = "default"
DescriptionAttrName string = "description"
MetadataAttrName string = "metadata"
// TODO: Fully migrate to overrides over rules
RulesAttrName string = "rules"
OverridesAttrName string = "overrides"
Expand All @@ -54,6 +55,7 @@ var (
OverridesAttrName: {},
validatorAttrName: {},
unitTestsAttrName: {},
MetadataAttrName: {},
}
)

Expand Down Expand Up @@ -153,6 +155,10 @@ func (fb *featureBuilder) Build() (*feature.CompiledFeature, error) {
return nil, errors.Wrap(err, "description")
}
f.Namespace = fb.namespace
f.Metadata, err = fb.getMetadata(featureVal)
if err != nil {
return nil, errors.Wrap(err, "metadata")
}

overrideVals, err := fb.addOverrides(f, featureVal)
if err != nil {
Expand Down Expand Up @@ -295,6 +301,23 @@ func (fb *featureBuilder) getDescription(featureVal *starlarkstruct.Struct) (str
return dsc.GoString(), nil
}

func (fb *featureBuilder) getMetadata(featureVal *starlarkstruct.Struct) (map[string]any, error) {
metadataVal, err := featureVal.Attr(MetadataAttrName)
if err != nil {
//lint:ignore nilerr `Struct.Attr` returns error if attribute doesn't exist
return nil, nil
}
metadataDict, ok := metadataVal.(*starlark.Dict)
if !ok {
return nil, fmt.Errorf("metadata must be a dict (got a %s)", metadataVal.Type())
}
metadataMap, err := translateContext(metadataDict)
if err != nil {
return nil, errors.Wrap(err, "translate metadata attribute")
}
return metadataMap, nil
}

func (fb *featureBuilder) addOverrides(f *feature.Feature, featureVal *starlarkstruct.Struct) ([]starlark.Value, error) {
overridesVal, err := featureVal.Attr(OverridesAttrName)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions pkg/star/static/traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
ResultVariableName string = "result"
DefaultValueAttrName string = "default"
DescriptionAttrName string = "description"
MetadataAttrName string = "metadata"
// TODO: Fully migrate to overrides over rules
RulesAttrName string = "rules"
OverridesAttrName string = "overrides"
Expand All @@ -40,11 +41,13 @@ func defaultNoop(v *build.Expr) error { return nil }
func descriptionNoop(v *build.StringExpr) error { return nil }
func rulesNoop(rules *overridesWrapper) error { return nil }
func importsNoop(imports *importsWrapper) error { return nil }
func metadataNoop(ast *starFeatureAST) error { return nil }

type defaultFn func(v *build.Expr) error
type descriptionFn func(v *build.StringExpr) error
type overridesFn func(rules *overridesWrapper) error
type importsFn func(imports *importsWrapper) error
type metadataFn func(ast *starFeatureAST) error

// Traverses a lekko starlark file, running methods on various
// components of the file. Methods can be provided to read the
Expand All @@ -57,6 +60,7 @@ type traverser struct {
descriptionFn descriptionFn
overridesFn overridesFn
protoImportsFn importsFn
metadataFn metadataFn
}

func newTraverser(f *build.File, nv feature.NamespaceVersion) *traverser {
Expand All @@ -67,6 +71,7 @@ func newTraverser(f *build.File, nv feature.NamespaceVersion) *traverser {
descriptionFn: descriptionNoop,
overridesFn: rulesNoop,
protoImportsFn: importsNoop,
metadataFn: metadataNoop,
}
}

Expand All @@ -90,6 +95,11 @@ func (t *traverser) withProtoImportsFn(fn importsFn) *traverser {
return t
}

func (t *traverser) withMetadataFn(fn metadataFn) *traverser {
t.metadataFn = fn
return t
}

func (t *traverser) traverse() error {
imports := t.getProtoImports()
if err := t.protoImportsFn(imports); err != nil {
Expand Down Expand Up @@ -118,6 +128,9 @@ func (t *traverser) traverse() error {
if err := t.descriptionFn(descriptionStr); err != nil {
return errors.Wrap(err, "description fn")
}
if err := t.metadataFn(ast); err != nil {
return errors.Wrap(err, "metadata fn")
}
// rules
if err := ast.parseOverrides(t.overridesFn, t.nv); err != nil {
return err
Expand Down
42 changes: 40 additions & 2 deletions pkg/star/static/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func (w *walker) Build() (*featurev1beta1.StaticFeature, error) {
withDefaultFn(w.buildDefaultFn(ret)).
withDescriptionFn(w.buildDescriptionFn(ret)).
withOverridesFn(w.buildRulesFn(ret)).
withProtoImportsFn(w.buildProtoImportsFn(ret))
withProtoImportsFn(w.buildProtoImportsFn(ret)).
withMetadataFn(w.buildMetadataFn(ret))

if err := t.traverse(); err != nil {
return nil, errors.Wrap(err, "traverse")
Expand All @@ -101,7 +102,8 @@ func (w *walker) Mutate(f *featurev1beta1.StaticFeature) ([]byte, error) {
t := newTraverser(ast, w.nv).
withDefaultFn(w.mutateDefaultFn(f)).
withDescriptionFn(w.mutateDescriptionFn(f)).
withOverridesFn(w.mutateOverridesFn(f))
withOverridesFn(w.mutateOverridesFn(f)).
withMetadataFn(w.mutateMetadataFn(f))

if err := t.traverse(); err != nil {
return nil, errors.Wrap(err, "traverse")
Expand Down Expand Up @@ -240,6 +242,27 @@ func (w *walker) buildDescriptionFn(f *featurev1beta1.StaticFeature) description
}
}

func (w *walker) buildMetadataFn(f *featurev1beta1.StaticFeature) metadataFn {
return func(ast *starFeatureAST) error {
metadataExprPtr, found := ast.get(MetadataAttrName)
if !found {
return nil
}
metadataExpr := *metadataExprPtr
metadataDict, ok := metadataExpr.(*build.DictExpr)
if !ok {
return errors.Wrapf(ErrUnsupportedStaticParsing, "metadata kwarg: expected dict, got %T", metadataExpr)
}
metadataValue, err := w.extractJSONValue(metadataDict)
if err != nil {
return errors.Wrap(err, "extract metadata")
}
f.Feature.Metadata = metadataValue
f.FeatureOld.Metadata = metadataValue
return nil
}
}

func (w *walker) buildRulesFn(f *featurev1beta1.StaticFeature) overridesFn {
return func(overridesW *overridesWrapper) error {
for i, o := range overridesW.overrides {
Expand Down Expand Up @@ -485,6 +508,21 @@ func (w *walker) mutateDescriptionFn(f *featurev1beta1.StaticFeature) descriptio
}
}

func (w *walker) mutateMetadataFn(f *featurev1beta1.StaticFeature) metadataFn {
return func(ast *starFeatureAST) error {
metadataProto := f.FeatureOld.GetMetadata()
if metadataProto == nil {
return nil
}
metadataStarDict, err := w.genJSONValue(metadataProto, nil)
if err != nil {
return err
}
ast.set(MetadataAttrName, metadataStarDict)
return nil
}
}

func (w *walker) mutateOverridesFn(f *featurev1beta1.StaticFeature) overridesFn {
return func(overridesW *overridesWrapper) error {
var newOverrides []override
Expand Down
2 changes: 2 additions & 0 deletions proto/lekko/feature/v1beta1/feature.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ syntax = "proto3";
package lekko.feature.v1beta1;

import "google/protobuf/any.proto";
import "google/protobuf/struct.proto";
import "lekko/rules/v1beta2/rules.proto";
import "lekko/rules/v1beta3/rules.proto";

Expand All @@ -30,6 +31,7 @@ message Feature {
string description = 2;
Tree tree = 3;
FeatureType type = 4;
google.protobuf.Value metadata = 7;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're enforcing that metadata needs to be a dict in starlark, can't we explicitly use the Struct type instead of Value?

}

// Enumerates the canonical types that lekko supports
Expand Down
2 changes: 2 additions & 0 deletions proto/lekko/feature/v1beta1/static.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ syntax = "proto3";

package lekko.feature.v1beta1;

import "google/protobuf/struct.proto";
import "lekko/feature/v1beta1/feature.proto";

// Represents a statically parsed feature.
Expand Down Expand Up @@ -47,6 +48,7 @@ message FeatureStruct {
string description = 2;
StarExpr default = 3;
Rules rules = 4;
google.protobuf.Value metadata = 7;
}

message Rules {
Expand Down