Skip to content

Commit

Permalink
proto value -> proto struct
Browse files Browse the repository at this point in the history
  • Loading branch information
passichenko committed Oct 20, 2023
1 parent 09685e5 commit bda6981
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 39 deletions.
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-20231019182200-bf5993a63215.1
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.31.0-20231019182200-bf5993a63215.1
buf.build/gen/go/lekkodev/cli/bufbuild/connect-go v1.10.0-20231020162356-e763402ec965.1
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.31.0-20231020162356-e763402ec965.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-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/cli/bufbuild/connect-go v1.10.0-20231020162356-e763402ec965.1 h1:iGIRelUi9fFwQ15Op90jP73+L8T0M8uewZeI6WXW8+s=
buf.build/gen/go/lekkodev/cli/bufbuild/connect-go v1.10.0-20231020162356-e763402ec965.1/go.mod h1:hJtG6Y49OSN1DhCYPGaCkfN1jibDflptgnHnLJo/Nl0=
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.31.0-20231020162356-e763402ec965.1 h1:YzjpTz4MgOa+EhI6lbIFLBAUTgQf2aVSIUoZlPBBv2k=
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.31.0-20231020162356-e763402ec965.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
82 changes: 49 additions & 33 deletions pkg/star/static/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,27 +213,35 @@ func (w *walker) extractJSONValue(v build.Expr) (*structpb.Value, error) {
}
return structpb.NewListValue(listVal), nil
case *build.DictExpr:
structVal := structpb.Struct{
Fields: map[string]*structpb.Value{},
}
for _, kvExpr := range t.List {
kvExpr := kvExpr
keyExpr, ok := kvExpr.Key.(*build.StringExpr)
if !ok {
return nil, errors.Wrapf(ErrUnsupportedStaticParsing, "json structs must have keys of type string, not %T", kvExpr.Key)
}
key := keyExpr.Value
vVar, err := w.extractJSONValue(kvExpr.Value)
if err != nil {
return nil, errors.Wrap(err, "extract struct elem value")
}
structVal.Fields[key] = vVar
structVal, err := w.extractJSONStruct(t)
if err != nil {
return nil, err
}
return structpb.NewStructValue(&structVal), nil
return structpb.NewStructValue(structVal), nil
}
return nil, errors.Wrapf(ErrUnsupportedStaticParsing, "type %T", v)
}

func (w *walker) extractJSONStruct(d *build.DictExpr) (*structpb.Struct, error) {
structVal := structpb.Struct{
Fields: map[string]*structpb.Value{},
}
for _, kvExpr := range d.List {
kvExpr := kvExpr
keyExpr, ok := kvExpr.Key.(*build.StringExpr)
if !ok {
return nil, errors.Wrapf(ErrUnsupportedStaticParsing, "json structs must have keys of type string, not %T", kvExpr.Key)
}
key := keyExpr.Value
vVar, err := w.extractJSONValue(kvExpr.Value)
if err != nil {
return nil, errors.Wrap(err, "extract struct elem value")
}
structVal.Fields[key] = vVar
}
return &structVal, nil
}

func (w *walker) buildDescriptionFn(f *featurev1beta1.StaticFeature) descriptionFn {
return func(v *build.StringExpr) error {
f.Feature.Description = v.Value
Expand All @@ -253,12 +261,12 @@ func (w *walker) buildMetadataFn(f *featurev1beta1.StaticFeature) metadataFn {
if !ok {
return errors.Wrapf(ErrUnsupportedStaticParsing, "metadata kwarg: expected dict, got %T", metadataExpr)
}
metadataValue, err := w.extractJSONValue(metadataDict)
metadataStruct, err := w.extractJSONStruct(metadataDict)
if err != nil {
return errors.Wrap(err, "extract metadata")
}
f.Feature.Metadata = metadataValue
f.FeatureOld.Metadata = metadataValue
f.Feature.Metadata = metadataStruct
f.FeatureOld.Metadata = metadataStruct
return nil
}
}
Expand Down Expand Up @@ -428,6 +436,24 @@ func (w *walker) genValue(a *anypb.Any, sf *featurev1beta1.StaticFeature, meta *
}
}

func (w *walker) genJSONStruct(s *structpb.Struct, meta *featurev1beta1.StarMeta) (*build.DictExpr, error) {
dictExpr := &build.DictExpr{
ForceMultiLine: true,
}
for key, value := range s.Fields {
valExpr, err := w.genJSONValue(value, meta)
if err != nil {
return nil, errors.Wrap(err, "gen value dict elem")
}
dictExpr.List = append(dictExpr.List, &build.KeyValueExpr{
Key: starString(key),
Value: valExpr,
})
}
sortKVs(dictExpr.List)
return dictExpr, nil
}

func (w *walker) genJSONValue(val *structpb.Value, meta *featurev1beta1.StarMeta) (build.Expr, error) {
switch k := val.Kind.(type) {
case *structpb.Value_NullValue:
Expand Down Expand Up @@ -457,20 +483,10 @@ func (w *walker) genJSONValue(val *structpb.Value, meta *featurev1beta1.StarMeta
}
return listExpr, nil
case *structpb.Value_StructValue:
dictExpr := &build.DictExpr{
ForceMultiLine: true,
}
for key, value := range k.StructValue.Fields {
valExpr, err := w.genJSONValue(value, meta)
if err != nil {
return nil, errors.Wrap(err, "gen value dict elem")
}
dictExpr.List = append(dictExpr.List, &build.KeyValueExpr{
Key: starString(key),
Value: valExpr,
})
dictExpr, err := w.genJSONStruct(k.StructValue, meta)
if err != nil {
return nil, err
}
sortKVs(dictExpr.List)
return dictExpr, nil
default:
return nil, errors.Wrapf(ErrUnsupportedStaticParsing, "structpb val type %T", k)
Expand Down Expand Up @@ -514,7 +530,7 @@ func (w *walker) mutateMetadataFn(f *featurev1beta1.StaticFeature) metadataFn {
if metadataProto == nil {
return nil
}
metadataStarDict, err := w.genJSONValue(metadataProto, nil)
metadataStarDict, err := w.genJSONStruct(metadataProto, nil)
if err != nil {
return err
}
Expand Down

0 comments on commit bda6981

Please sign in to comment.