@@ -32,6 +32,7 @@ import (
32
32
"github.com/lekkodev/cli/pkg/secrets"
33
33
"github.com/pkg/errors"
34
34
"github.com/spf13/cobra"
35
+ strcase "github.com/stoewer/go-strcase"
35
36
"golang.org/x/mod/modfile"
36
37
"google.golang.org/protobuf/encoding/protojson"
37
38
"google.golang.org/protobuf/proto"
@@ -60,9 +61,9 @@ func genGoCmd() *cobra.Command {
60
61
if err != nil {
61
62
return errors .Wrap (err , "new repo" )
62
63
}
63
- _ , nsMDs , err := r .ParseMetadata (cmd .Context (), ns )
64
+ _ , nsMDs := try . To2 ( r .ParseMetadata (cmd .Context ()) )
64
65
65
- staticCtxType := unpackProtoType (moduleRoot , "/" + nsMDs [ns ])
66
+ staticCtxType := unpackProtoType (moduleRoot , nsMDs [ns ]. ContextProto )
66
67
ffs , err := r .GetFeatureFiles (cmd .Context (), ns )
67
68
if err != nil {
68
69
return err
@@ -73,6 +74,9 @@ func genGoCmd() *cobra.Command {
73
74
var protoAsByteStrings []string
74
75
var codeStrings []string
75
76
protoImportSet := make (map [string ]* protoImport )
77
+ if staticCtxType != nil {
78
+ protoImportSet [staticCtxType .ImportPath ] = staticCtxType
79
+ }
76
80
for _ , ff := range ffs {
77
81
fff , err := os .ReadFile (wd + "/" + ns + "/" + ff .CompiledProtoBinFileName )
78
82
if err != nil {
@@ -82,13 +86,13 @@ func genGoCmd() *cobra.Command {
82
86
if err := proto .Unmarshal (fff , f ); err != nil {
83
87
return err
84
88
}
85
- codeString , err := genGoForFeature (f , ns )
89
+ codeString , err := genGoForFeature (f , ns , staticCtxType )
86
90
if err != nil {
87
91
return err
88
92
}
89
93
if f .Type == featurev1beta1 .FeatureType_FEATURE_TYPE_PROTO {
90
94
protoImport := unpackProtoType (moduleRoot , f .Tree .Default .TypeUrl )
91
- protoImportSet [protoImport .ImportPath ] = & protoImport
95
+ protoImportSet [protoImport .ImportPath ] = protoImport
92
96
}
93
97
protoAsBytes := fmt .Sprintf ("\t \t \" %s\" : []byte{" , f .Key )
94
98
for idx , b := range fff {
@@ -173,7 +177,6 @@ var StaticConfig = map[string]map[string][]byte{
173
177
"--include-imports" ,
174
178
wd ) // #nosec G204
175
179
pCmd .Dir = "."
176
- fmt .Println ("executing in working dir: " + wd + " command: " + pCmd .String ())
177
180
if out , err := pCmd .CombinedOutput (); err != nil {
178
181
fmt .Println ("this is the error probably" )
179
182
fmt .Println (string (out ))
@@ -217,16 +220,16 @@ var genCmd = &cobra.Command{
217
220
Short : "generate library code from configs" ,
218
221
}
219
222
220
- func genGoForFeature (f * featurev1beta1.Feature , ns string ) (string , error ) {
223
+ func genGoForFeature (f * featurev1beta1.Feature , ns string , staticCtxType * protoImport ) (string , error ) {
221
224
const defaultTemplateBody = `// {{$.Description}}
222
225
func (c *LekkoClient) {{$.FuncName}}(ctx context.Context) ({{$.RetType}}, error) {
223
226
return c.{{$.GetFunction}}(ctx, "{{$.Namespace}}", "{{$.Key}}")
224
227
}
225
228
226
229
// {{$.Description}}
227
- func (c *SafeLekkoClient) {{$.FuncName}}(ctx context.Context ) {{$.RetType}} {
228
- {{if $.NaturalLanguage}}{{ range $.NaturalLanguage}}{{ . }}
229
- {{end}}{{else}}
230
+ {{if $.NaturalLanguage}} func (c *SafeLekkoClient) {{$.FuncName}}(ctx *{{$.StaticType}} ) {{$.RetType}} {
231
+ {{range $.NaturalLanguage}}{{ . }}
232
+ {{end}}{{else}}func (c *SafeLekkoClient) {{$.FuncName}}(ctx context.Context) {{$.RetType}} {
230
233
return c.{{$.GetFunction}}(ctx, "{{$.Namespace}}", "{{$.Key}}")
231
234
{{end}}}`
232
235
@@ -264,6 +267,7 @@ func (c *SafeLekkoClient) {{$.FuncName}}(ctx context.Context, result interface{}
264
267
var getFunction string
265
268
templateBody := defaultTemplateBody
266
269
var natty []string
270
+
267
271
switch f .Type {
268
272
case 1 :
269
273
retType = "bool"
@@ -298,6 +302,7 @@ func (c *SafeLekkoClient) {{$.FuncName}}(ctx context.Context, result interface{}
298
302
Namespace string
299
303
Key string
300
304
NaturalLanguage []string
305
+ StaticType string
301
306
}{
302
307
f .Description ,
303
308
funcName ,
@@ -306,6 +311,7 @@ func (c *SafeLekkoClient) {{$.FuncName}}(ctx context.Context, result interface{}
306
311
ns ,
307
312
f .Key ,
308
313
natty ,
314
+ fmt .Sprintf ("%s.%s" , staticCtxType .PackageAlias , staticCtxType .Type ),
309
315
}
310
316
templ , err := template .New ("go func" ).Parse (templateBody )
311
317
if err != nil {
@@ -322,28 +328,41 @@ type protoImport struct {
322
328
Type string
323
329
}
324
330
325
- func unpackProtoType (moduleRoot string , typeURL string ) protoImport {
331
+ // This function handles both the google.protobuf.Any.TypeURL variable
332
+ // which has the format of `types.googleapis.com/fully.qualified.Proto`
333
+ // and purely `fully.qualified.Proto`
334
+ //
335
+ // return nil if typeURL is empty. Panics on any problems like the rest of the file.
336
+ func unpackProtoType (moduleRoot string , typeURL string ) * protoImport {
337
+ if typeURL == "" {
338
+ return nil
339
+ }
326
340
anyURLSplit := strings .Split (typeURL , "/" )
327
- if anyURLSplit [0 ] != "type.googleapis.com" {
328
- panic ("invalid any type url: " + typeURL )
341
+ fqType := anyURLSplit [0 ]
342
+ if len (anyURLSplit ) > 1 {
343
+ if anyURLSplit [0 ] != "type.googleapis.com" {
344
+ panic ("invalid any type url: " + typeURL )
345
+ }
346
+ fqType = anyURLSplit [1 ]
329
347
}
348
+
330
349
// turn default.config.v1beta1.DBConfig into:
331
350
// moduleRoot/internal/lekko/proto/default/config/v1beta1
332
- typeParts := strings .Split (anyURLSplit [ 1 ] , "." )
351
+ typeParts := strings .Split (fqType , "." )
333
352
334
353
importPath := strings .Join (append ([]string {moduleRoot + "/internal/lekko/proto" }, typeParts [:len (typeParts )- 1 ]... ), "/" )
335
354
336
355
prefix := fmt .Sprintf (`%s%s` , typeParts [len (typeParts )- 3 ], typeParts [len (typeParts )- 2 ])
337
356
338
357
// TODO do google.protobuf.X
339
- switch anyURLSplit [ 1 ] {
358
+ switch fqType {
340
359
case "google.protobuf.Duration" :
341
360
importPath = "google.golang.org/protobuf/types/known/durationpb"
342
361
prefix = "durationpb"
343
362
default :
344
363
}
345
364
346
- return protoImport {PackageAlias : prefix , ImportPath : importPath , Type : typeParts [len (typeParts )- 1 ]}
365
+ return & protoImport {PackageAlias : prefix , ImportPath : importPath , Type : typeParts [len (typeParts )- 1 ]}
347
366
}
348
367
349
368
func translateFeature (f * featurev1beta1.Feature ) []string {
@@ -355,7 +374,9 @@ func translateFeature(f *featurev1beta1.Feature) []string {
355
374
}
356
375
rule := translateRule (constraint .GetRuleAstNew ())
357
376
buffer = append (buffer , fmt .Sprintf ("\t %s %s {" , ifToken , rule ))
377
+
358
378
// TODO this doesn't work for proto, but let's try
379
+
359
380
buffer = append (buffer , fmt .Sprintf ("\t \t return %s" , try .To1 (protojson .Marshal (try .To1 (constraint .Value .UnmarshalNew ())))))
360
381
}
361
382
if len (f .Tree .Constraints ) > 0 {
@@ -377,7 +398,7 @@ func translateRule(rule *rulesv1beta3.Rule) string {
377
398
if err != nil {
378
399
panic (err )
379
400
}
380
- return fmt .Sprintf ("%s == %s" , v .Atom .ContextKey , string (b ))
401
+ return fmt .Sprintf ("ctx. %s == %s" , strcase . UpperCamelCase ( v .Atom .ContextKey ) , string (b ))
381
402
case rulesv1beta3 .ComparisonOperator_COMPARISON_OPERATOR_CONTAINED_WITHIN :
382
403
// TODO, probably logical to have this here but we need slice syntax, use slices as of golang 1.21
383
404
}
0 commit comments