Skip to content

Commit

Permalink
lint + extract metadata when compiling from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
passichenko committed Oct 19, 2023
1 parent 867ec3e commit 5809974
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
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
11 changes: 8 additions & 3 deletions pkg/star/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,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 @@ -297,10 +301,11 @@ func (fb *featureBuilder) getDescription(featureVal *starlarkstruct.Struct) (str
return dsc.GoString(), nil
}

func (fb *featureBuilder) getMetadata(featureVal *starlarkstruct.Struct) (*structpb.Value, error) {
func (fb *featureBuilder) getMetadata(featureVal *starlarkstruct.Struct) (map[string]any, error) {
metadataVal, err := featureVal.Attr(MetadataAttrName)
if err != nil {
return nil, errors.Wrap(err, "metadata attribute")
//lint:ignore nilerr `Struct.Attr` returns error if attribute doesn't exist
return nil, nil
}
metadataDict, ok := metadataVal.(*starlark.Dict)
if !ok {
Expand All @@ -310,7 +315,7 @@ func (fb *featureBuilder) getMetadata(featureVal *starlarkstruct.Struct) (*struc
if err != nil {
return nil, errors.Wrap(err, "translate metadata attribute")
}
return structpb.NewValue(metadataMap)
return metadataMap, nil
}

func (fb *featureBuilder) addOverrides(f *feature.Feature, featureVal *starlarkstruct.Struct) ([]starlark.Value, error) {
Expand Down
10 changes: 3 additions & 7 deletions pkg/star/static/traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ 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}
func metadataNoop(ast *starFeatureAST) error { return nil }

type defaultFn func(v *build.Expr) error
type descriptionFn func(v *build.StringExpr) error
Expand Down Expand Up @@ -71,7 +71,7 @@ func newTraverser(f *build.File, nv feature.NamespaceVersion) *traverser {
descriptionFn: descriptionNoop,
overridesFn: rulesNoop,
protoImportsFn: importsNoop,
metadataFn: metadataNoop,
metadataFn: metadataNoop,
}
}

Expand All @@ -98,7 +98,7 @@ func (t *traverser) withProtoImportsFn(fn importsFn) *traverser {
func (t *traverser) withMetadataFn(fn metadataFn) *traverser {
t.metadataFn = fn
return t
}
}

func (t *traverser) traverse() error {
imports := t.getProtoImports()
Expand Down Expand Up @@ -339,10 +339,6 @@ type importVal struct {
assignExpr *build.AssignExpr
}

type metadataWrapper struct {
metadataExpr *build.DictExpr
}

func newOverride(li build.Expr) (*override, error) {
tupleV, ok := li.(*build.TupleExpr)
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion pkg/star/static/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ func (w *walker) mutateMetadataFn(f *featurev1beta1.StaticFeature) metadataFn {
if metadataProto == nil {
return nil
}
metadataStarDict, err := w.genJSONValue(metadataProto, nil)
metadataStarDict, err := w.genJSONValue(metadataProto, nil)
if err != nil {
return err
}
Expand Down

0 comments on commit 5809974

Please sign in to comment.