From d1c2715ad66a659cf3f5d5c571f05e4d6de25c62 Mon Sep 17 00:00:00 2001 From: Jonathan Dobbie Date: Wed, 25 Sep 2024 17:30:49 -0700 Subject: [PATCH] Super basic argless calls --- cmd/lekko/sync_test.go | 23 +++++++++++++++++++++++ pkg/gen/ts.go | 15 +++++++++++++++ pkg/sync/ts.go | 6 +++--- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/cmd/lekko/sync_test.go b/cmd/lekko/sync_test.go index b564dad7..b3775b5d 100644 --- a/cmd/lekko/sync_test.go +++ b/cmd/lekko/sync_test.go @@ -28,6 +28,7 @@ import ( "testing" "github.com/lekkodev/cli/pkg/gen" + protoutils "github.com/lekkodev/cli/pkg/proto" "github.com/lekkodev/cli/pkg/sync" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/proto" @@ -223,3 +224,25 @@ func Test_ProtoJsonToTs(t *testing.T) { }) } */ + +func Test_blobToTs(t *testing.T) { + repoContents, err := sync.SyncTSOutputToRepositoryContents([]byte("Cp0CCgdkZWZhdWx0EnUKDWJhbm5lci1jb25maWcaYgovCi10eXBlLmdvb2dsZWFwaXMuY29tL2dvb2dsZS5wcm90b2J1Zi5Cb29sVmFsdWUaLwotdHlwZS5nb29nbGVhcGlzLmNvbS9nb29nbGUucHJvdG9idWYuQm9vbFZhbHVlIAESmgEKBXRoaW5nGo4BCkUKMnR5cGUuZ29vZ2xlYXBpcy5jb20vbGVra28ucnVsZXMudjFiZXRhMy5Db25maWdDYWxsEg8aDWJhbm5lci1jb25maWcaRQoydHlwZS5nb29nbGVhcGlzLmNvbS9sZWtrby5ydWxlcy52MWJldGEzLkNvbmZpZ0NhbGwSDxoNYmFubmVyLWNvbmZpZyABEuwLCvsBCh5nb29nbGUvcHJvdG9idWYvZHVyYXRpb24ucHJvdG8SD2dvb2dsZS5wcm90b2J1ZiI6CghEdXJhdGlvbhIYCgdzZWNvbmRzGAEgASgDUgdzZWNvbmRzEhQKBW5hbm9zGAIgASgFUgVuYW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hbmcub3JnL3Byb3RvYnVmL3R5cGVzL2tub3duL3N0cnVjdHBi+AEBogIDR1BCqgIeR29vZ2xlLlByb3RvYnVmLldlbGxLbm93blR5cGVzYgZwcm90bzMKhgQKHmdvb2dsZS9wcm90b2J1Zi93cmFwcGVycy5wcm90bxIPZ29vZ2xlLnByb3RvYnVmIiMKC0RvdWJsZVZhbHVlEhQKBXZhbHVlGAEgASgBUgV2YWx1ZSIiCgpGbG9hdFZhbHVlEhQKBXZhbHVlGAEgASgCUgV2YWx1ZSIiCgpJbnQ2NFZhbHVlEhQKBXZhbHVlGAEgASgDUgV2YWx1ZSIjCgtVSW50NjRWYWx1ZRIUCgV2YWx1ZRgBIAEoBFIFdmFsdWUiIgoKSW50MzJWYWx1ZRIUCgV2YWx1ZRgBIAEoBVIFdmFsdWUiIwoLVUludDMyVmFsdWUSFAoFdmFsdWUYASABKA1SBXZhbHVlIiEKCUJvb2xWYWx1ZRIUCgV2YWx1ZRgBIAEoCFIFdmFsdWUiIwoLU3RyaW5nVmFsdWUSFAoFdmFsdWUYASABKAlSBXZhbHVlIiIKCkJ5dGVzVmFsdWUSFAoFdmFsdWUYASABKAxSBXZhbHVlQoMBChNjb20uZ29vZ2xlLnByb3RvYnVmQg1XcmFwcGVyc1Byb3RvUAFaMWdvb2dsZS5nb2xhbmcub3JnL3Byb3RvYnVmL3R5cGVzL2tub3duL3dyYXBwZXJzcGL4AQGiAgNHUEKqAh5Hb29nbGUuUHJvdG9idWYuV2VsbEtub3duVHlwZXNiBnByb3RvMw==")) + if err != nil { + panic(err) + } + + typeRegistry, err := protoutils.FileDescriptorSetToTypeRegistry(repoContents.FileDescriptorSet) + if err != nil { + panic(err) + } + gen.TypeRegistry = typeRegistry + for _, namespace := range repoContents.Namespaces { + for _, f := range namespace.Features { + code, err := gen.GenTSForFeature(f, namespace.Name, "") + if err != nil { + panic(err) + } + fmt.Println(code) + } + } +} diff --git a/pkg/gen/ts.go b/pkg/gen/ts.go index ceb30083..c2457e3e 100644 --- a/pkg/gen/ts.go +++ b/pkg/gen/ts.go @@ -575,6 +575,21 @@ func FieldValueToTS(f protoreflect.FieldDescriptor, val protoreflect.Value) stri } func translateRetValueTS(val *anypb.Any, t featurev1beta1.FeatureType) string { + // TODO - move to lekkoAny + if val.TypeUrl == "type.googleapis.com/lekko.rules.v1beta3.ConfigCall" { + call := &rulesv1beta3.ConfigCall{} + err := proto.Unmarshal(val.Value, call) + if err != nil { + panic(err) + } + var funcNameBuilder strings.Builder + funcNameBuilder.WriteString("get") + for _, word := range regexp.MustCompile("[_-]+").Split(call.Key, -1) { + funcNameBuilder.WriteString(strings.ToUpper(word[:1]) + word[1:]) + } + funcName := funcNameBuilder.String() + return fmt.Sprintf("%s()", funcName) + } //var dypb *dynamicpb.Message marshalOptions := protojson.MarshalOptions{ UseProtoNames: true, diff --git a/pkg/sync/ts.go b/pkg/sync/ts.go index 067c8df2..aed090ce 100644 --- a/pkg/sync/ts.go +++ b/pkg/sync/ts.go @@ -56,7 +56,7 @@ func SyncTS(lekkoPath string) (*featurev1beta1.RepositoryContents, error) { if err != nil { return nil, errors.Wrapf(err, "sync ts: %s", output) } - return syncTSOutputToRepositoryContents(output) + return SyncTSOutputToRepositoryContents(output) } func SyncTSFiles(lekkoFiles ...string) (*featurev1beta1.RepositoryContents, error) { @@ -66,11 +66,11 @@ func SyncTSFiles(lekkoFiles ...string) (*featurev1beta1.RepositoryContents, erro if err != nil { return nil, errors.Wrapf(err, "sync ts: %s", output) } - return syncTSOutputToRepositoryContents(output) + return SyncTSOutputToRepositoryContents(output) } // Output is expected to be base64 encoded serialized RepositoryContents message -func syncTSOutputToRepositoryContents(output []byte) (*featurev1beta1.RepositoryContents, error) { +func SyncTSOutputToRepositoryContents(output []byte) (*featurev1beta1.RepositoryContents, error) { // Because Protobuf is not self-describing, we have to jump through some hoops here for deserialization. // The RepositoryContents message contains the FDS which we want to use as the resolver for unmarshalling // the rest of the contents.