From bda6981643f443d9e437c9da38575c8df7e34506 Mon Sep 17 00:00:00 2001 From: Sergey Passichenko Date: Fri, 20 Oct 2023 09:44:21 -0700 Subject: [PATCH] proto value -> proto struct --- go.mod | 4 +- go.sum | 8 ++-- pkg/star/static/walk.go | 82 ++++++++++++++++++++++++----------------- 3 files changed, 55 insertions(+), 39 deletions(-) diff --git a/go.mod b/go.mod index f849ced8..ce6dd935 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index f3829f5b..1f7e716a 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/star/static/walk.go b/pkg/star/static/walk.go index 8816a9bf..1eb43ea2 100644 --- a/pkg/star/static/walk.go +++ b/pkg/star/static/walk.go @@ -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 @@ -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 } } @@ -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: @@ -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) @@ -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 }