Skip to content

Commit

Permalink
Repoless gen for TS (#462)
Browse files Browse the repository at this point in the history
* Repoless gen for TS

* Lint
  • Loading branch information
DavidSGK authored Sep 26, 2024
1 parent 606b971 commit 4174dc4
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 146 deletions.
28 changes: 24 additions & 4 deletions cmd/lekko/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func genNative(ctx context.Context, project *native.Project, lekkoPath, repoPath
return gen.GenNative(ctx, project, lekkoPath, repoPath, opts)
}

// TODO: Add option to read encoded repo contents like gen ts
func genGoCmd() *cobra.Command {
var lekkoPath, repoPath, ns string
var initMode bool
Expand All @@ -99,22 +100,41 @@ func genGoCmd() *cobra.Command {
}

func genTSCmd() *cobra.Command {
var ns string
var repoPath string
var lekkoPath string
var lekkoPath, repoPath, encodedRepoContents, ns string
cmd := &cobra.Command{
Use: "ts",
Short: "generate TypeScript library code from lekkos",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
// For now, output generated code instead of writing to repo if using encoded contents
// TODO: make this optional even for local-based
if len(encodedRepoContents) > 0 {
repoContents, err := repo.DecodeRepositoryContents([]byte(encodedRepoContents))
if err != nil {
return errors.Wrap(err, "decode")
}
generated, err := gen.GenTS(repoContents, ns)
if err != nil {
return errors.Wrap(err, "gen")
}
formatted, err := gen.FormatTS(generated)
if err != nil {
return errors.Wrap(err, "format")
}
fmt.Println(formatted)
return nil
}

nlProject := try.To1(native.DetectNativeLang(""))
if nlProject.Language != native.LangTypeScript {
return errors.Errorf("not a TypeScript project, detected %v instead", nlProject.Language)
}
return genNative(cmd.Context(), nlProject, lekkoPath, repoPath, ns, false)
return genNative(ctx, nlProject, lekkoPath, repoPath, ns, false)
},
}
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "path to Lekko native config files, will use autodetect if not set")
cmd.Flags().StringVarP(&repoPath, "repo-path", "r", "", "path to config repository, will use autodetect if not set")
cmd.Flags().StringVarP(&encodedRepoContents, "repo-contents", "R", "", "base64-encoded serialized repository contents, will use repo-path if not set")
cmd.Flags().StringVarP(&ns, "namespace", "n", "default", "namespace to generate code from")
return cmd
}
Expand Down
15 changes: 6 additions & 9 deletions cmd/lekko/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,11 @@ func isSame(ctx context.Context, existing map[string]map[string]*featurev1beta1.
} else {
// These might still be equal, because the typescript path combines logical things in ways that the go path does not
// Using ts since it has fewer args..
gen.TypeRegistry = registry
o, err := gen.GenTSForFeature(f, namespace.Name, "")
o, err := gen.GenTSForFeature(f, namespace.Name, "", registry)
if err != nil {
return false, err
}
e, err := gen.GenTSForFeature(existingConfig, namespace.Name, "")
e, err := gen.GenTSForFeature(existingConfig, namespace.Name, "", registry)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -378,13 +377,12 @@ func isSameTS(ctx context.Context, existing map[string]map[string]*featurev1beta
} else {
// These might still be equal, because the typescript path combines logical things in ways that the go path does not
// Using ts since it has fewer args..
gen.TypeRegistry = registry
//fmt.Printf("%+v\n\n", f)
o, err := gen.GenTSForFeature(f, namespace.Name, "")
o, err := gen.GenTSForFeature(f, namespace.Name, "", registry)
if err != nil {
return false, err
}
e, err := gen.GenTSForFeature(existingConfig, namespace.Name, "")
e, err := gen.GenTSForFeature(existingConfig, namespace.Name, "", registry)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -583,7 +581,6 @@ func ProtoJSONToTS(nsString []byte, fdString []byte) (string, error) {
if err != nil {
return "", err
}
gen.TypeRegistry = registry.Types
var featureStrings []string
for _, namespace := range namespaces.Namespaces {
for _, c := range namespace.Configs {
Expand All @@ -598,7 +595,7 @@ func ProtoJSONToTS(nsString []byte, fdString []byte) (string, error) {
}

var ourParameters string
sigType, err := gen.TypeRegistry.FindMessageByName(protoreflect.FullName(namespace.Name + ".config.v1beta1." + strcase.ToCamel(f.Key) + "Args"))
sigType, err := registry.Types.FindMessageByName(protoreflect.FullName(namespace.Name + ".config.v1beta1." + strcase.ToCamel(f.Key) + "Args"))
if err == nil {
d := sigType.Descriptor()
var varNames []string
Expand All @@ -613,7 +610,7 @@ func ProtoJSONToTS(nsString []byte, fdString []byte) (string, error) {
ourParameters = fmt.Sprintf("{%s}: {%s}", strings.Join(varNames, ", "), strings.Join(fields, " "))
}

fs, err := gen.GenTSForFeature(f, namespace.Name, ourParameters)
fs, err := gen.GenTSForFeature(f, namespace.Name, ourParameters, registry.Types)
featureStrings = append(featureStrings, fs)
if err != nil {
return "", err
Expand Down
3 changes: 2 additions & 1 deletion pkg/gen/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func NewGoGenerator(moduleRoot, outputPath, lekkoPath string, repoContents *feat
func NewGoGeneratorFromLocal(ctx context.Context, moduleRoot, outputPath, lekkoPath string, repoPath string) (*goGenerator, error) {
repoContents, err := ReadRepoContents(ctx, repoPath)
if err != nil {
return nil, errors.Wrapf(err, "read contents from %s", repoContents)
return nil, errors.Wrapf(err, "read contents from %s", repoPath)
}
typeRegistry, err := protoutils.FileDescriptorSetToTypeRegistry(repoContents.FileDescriptorSet)
if err != nil {
Expand Down Expand Up @@ -326,6 +326,7 @@ func renderGoTemplate(templateBody string, fileName string, data any) (string, e
// Generates public and private function files for the namespace as well as the overall client file.
// Writes outputs to the output paths specified in the construction args.
// TODO: since generator takes in whole repo contents now, could generate for all/filtered namespaces
// TODO: split away write out functionality and/or return contents
func (g *goGenerator) Gen(ctx context.Context, namespaceName string) (err error) {
defer err2.Handle(&err)
// Validate namespace
Expand Down
Loading

0 comments on commit 4174dc4

Please sign in to comment.