Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into tsGroupGen
Browse files Browse the repository at this point in the history
  • Loading branch information
lekko-jonathan committed Sep 27, 2024
2 parents d1c2715 + 6403b17 commit ee672e9
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 194 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
2 changes: 1 addition & 1 deletion cmd/lekko/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"os"
"strconv"

"connectrpc.com/connect"
"github.com/AlecAivazis/survey/v2"
"github.com/bufbuild/connect-go"
"github.com/lainio/err2"
"github.com/pkg/errors"
"github.com/spf13/cobra"
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
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ go 1.22
replace github.com/bazelbuild/buildtools => github.com/lekkodev/buildtools v0.0.0-20240325231538-96eefd799042

require (
buf.build/gen/go/lekkodev/cli/bufbuild/connect-go v1.10.0-20240528213244-5fdc18b47eea.1
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.34.2-20240923164736-6b09ba83efbf.2
buf.build/gen/go/lekkodev/cli/connectrpc/go v1.17.0-20240926040046-3e1042256cdf.1
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.34.2-20240926040046-3e1042256cdf.2
connectrpc.com/connect v1.17.0
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/atotto/clipboard v0.1.4
github.com/bazelbuild/buildtools v0.0.0-20220907133145-b9bfff5d7f91
github.com/briandowns/spinner v1.23.0
github.com/bufbuild/connect-go v1.10.0
github.com/cli/browser v1.0.0
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.12.0
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
buf.build/gen/go/lekkodev/cli/bufbuild/connect-go v1.10.0-20240528213244-5fdc18b47eea.1 h1:JqArhl+OClAdLQis1N2N6WmLv96CbOaNrQEYWL2ntlI=
buf.build/gen/go/lekkodev/cli/bufbuild/connect-go v1.10.0-20240528213244-5fdc18b47eea.1/go.mod h1:gkMKhhTCMDLJVmimyqao6P3g7jB7wDe3r7u8hV2iShE=
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.34.2-20240923164736-6b09ba83efbf.2 h1:JEiSyCH0wTycYIeIpm8xs/HcyPgHtJpdKBrUer8Jq6E=
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.34.2-20240923164736-6b09ba83efbf.2/go.mod h1:j/ek65dWz+D5GM7p9QUiHQj5X5gtRUMfGl1+GpSfm6g=
buf.build/gen/go/lekkodev/cli/connectrpc/go v1.17.0-20240926040046-3e1042256cdf.1 h1:agiFYCNijwOenKQtub2FQ3lUdZvXQTii7eVV7ByY6h4=
buf.build/gen/go/lekkodev/cli/connectrpc/go v1.17.0-20240926040046-3e1042256cdf.1/go.mod h1:1ia6DcIYiqkQNmSTA00+Kys2yObN8ByxgcLbaTY1hNo=
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.34.2-20240926040046-3e1042256cdf.2 h1:sS7KHlg3fCpYqz52WrRAOEr7uxezWXoGM7gopqB2gYQ=
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.34.2-20240926040046-3e1042256cdf.2/go.mod h1:j/ek65dWz+D5GM7p9QUiHQj5X5gtRUMfGl1+GpSfm6g=
buf.build/gen/go/lekkodev/sdk/protocolbuffers/go v1.34.2-20230810202034-1c821065b9a0.2 h1:ZEir2Lbw+XH5Dlnqiv0FUc8hC7QdUMpGSCIiREMriJ0=
buf.build/gen/go/lekkodev/sdk/protocolbuffers/go v1.34.2-20230810202034-1c821065b9a0.2/go.mod h1:YAvVDcY/tXuUXkpfm3LHCD6vz9SPv73CktuPGgqzJkI=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
connectrpc.com/connect v1.17.0 h1:W0ZqMhtVzn9Zhn2yATuUokDLO5N+gIuBWMOnsQrfmZk=
connectrpc.com/connect v1.17.0/go.mod h1:0292hj1rnx8oFrStN7cB4jjVBeqs+Yx5yDIC2prWDO8=
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
github.com/AlecAivazis/survey/v2 v2.3.6 h1:NvTuVHISgTHEHeBFqt6BHOe4Ny/NwGZr7w+F8S9ziyw=
Expand All @@ -31,8 +33,6 @@ github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/briandowns/spinner v1.23.0 h1:alDF2guRWqa/FOZZYWjlMIx2L6H0wyewPxo/CH4Pt2A=
github.com/briandowns/spinner v1.23.0/go.mod h1:rPG4gmXeN3wQV/TsAY4w8lPdIM6RX3yqeBQJSrbXjuE=
github.com/bufbuild/connect-go v1.10.0 h1:QAJ3G9A1OYQW2Jbk3DeoJbkCxuKArrvZgDt47mjdTbg=
github.com/bufbuild/connect-go v1.10.0/go.mod h1:CAIePUgkDR5pAFaylSMtNK45ANQjp9JvpluG20rhpV8=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
Expand Down
11 changes: 7 additions & 4 deletions make/go/dep_protoc_gen_connect_go.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ $(call _assert_var,CACHE_VERSIONS)
$(call _assert_var,CACHE_BIN)

# Settable
# https://github.com/bufbuild/connect-go 20220531 checked 20220601
CONNECT_VERSION ?= v0.1.0
# https://github.com/connectrpc/connect-go 20240920 checked 20240920
CONNECT_VERSION ?= v1.17.0

GO_GET_PKGS := $(GO_GET_PKGS) \
connectrpc.com/connect@$(CONNECT_VERSION)

PROTOC_GEN_CONNECT_GO := $(CACHE_VERSIONS)/connect-go/$(CONNECT_VERSION)
$(PROTOC_GEN_CONNECT_GO):
@rm -f $(CACHE_BIN)/connect-go
GOBIN=$(CACHE_BIN) go install github.com/bufbuild/connect-go/cmd/protoc-gen-connect-go@$(CONNECT_VERSION)
@rm -f $(CACHE_BIN)/protoc-gen-connect-go
GOBIN=$(CACHE_BIN) go install connectrpc.com/connect/cmd/protoc-gen-connect-go@$(CONNECT_VERSION)
@rm -rf $(dir $(PROTOC_GEN_CONNECT_GO))
@mkdir -p $(dir $(PROTOC_GEN_CONNECT_GO))
@touch $(PROTOC_GEN_CONNECT_GO)
Expand Down
12 changes: 6 additions & 6 deletions pkg/apikey/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ package apikey
import (
"context"

bffv1beta1connect "buf.build/gen/go/lekkodev/cli/bufbuild/connect-go/lekko/bff/v1beta1/bffv1beta1connect"
bffv1beta1connect "buf.build/gen/go/lekkodev/cli/connectrpc/go/lekko/bff/v1beta1/bffv1beta1connect"
bffv1beta1 "buf.build/gen/go/lekkodev/cli/protocolbuffers/go/lekko/bff/v1beta1"
"connectrpc.com/connect"

connect_go "github.com/bufbuild/connect-go"
"github.com/pkg/errors"
)

Expand All @@ -34,7 +34,7 @@ func NewAPIKey(bff bffv1beta1connect.BFFServiceClient) *APIKeyManager {
}

func (a *APIKeyManager) Create(ctx context.Context, teamname, nickname string) (*bffv1beta1.GenerateAPIKeyResponse, error) {
resp, err := a.bff.GenerateAPIKey(ctx, connect_go.NewRequest(&bffv1beta1.GenerateAPIKeyRequest{
resp, err := a.bff.GenerateAPIKey(ctx, connect.NewRequest(&bffv1beta1.GenerateAPIKeyRequest{
Nickname: nickname,
TeamName: teamname,
}))
Expand All @@ -45,15 +45,15 @@ func (a *APIKeyManager) Create(ctx context.Context, teamname, nickname string) (
}

func (a *APIKeyManager) List(ctx context.Context, teamname string) ([]*bffv1beta1.APIKey, error) {
resp, err := a.bff.ListAPIKeys(ctx, connect_go.NewRequest(&bffv1beta1.ListAPIKeysRequest{TeamName: teamname}))
resp, err := a.bff.ListAPIKeys(ctx, connect.NewRequest(&bffv1beta1.ListAPIKeysRequest{TeamName: teamname}))
if err != nil {
return nil, errors.Wrap(err, "list api keys")
}
return resp.Msg.GetApiKeys(), nil
}

func (a *APIKeyManager) Check(ctx context.Context, apikey string) (*bffv1beta1.APIKey, error) {
resp, err := a.bff.CheckAPIKey(ctx, connect_go.NewRequest(&bffv1beta1.CheckAPIKeyRequest{
resp, err := a.bff.CheckAPIKey(ctx, connect.NewRequest(&bffv1beta1.CheckAPIKeyRequest{
ApiKey: apikey,
}))
if err != nil {
Expand All @@ -63,7 +63,7 @@ func (a *APIKeyManager) Check(ctx context.Context, apikey string) (*bffv1beta1.A
}

func (a *APIKeyManager) Delete(ctx context.Context, nickname string) error {
_, err := a.bff.DeleteAPIKey(ctx, connect_go.NewRequest(&bffv1beta1.DeleteAPIKeyRequest{
_, err := a.bff.DeleteAPIKey(ctx, connect.NewRequest(&bffv1beta1.DeleteAPIKeyRequest{
Nickname: nickname,
}))
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 ee672e9

Please sign in to comment.