Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update most of Go native lang to be repoless #459

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 25 additions & 50 deletions cmd/lekko/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"path/filepath"
"strings"

"golang.org/x/mod/modfile"

"github.com/iancoleman/strcase"
"github.com/lainio/err2"
"github.com/lainio/err2/try"
Expand All @@ -39,7 +37,6 @@ import (
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/dynamicpb"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/durationpb"

bffv1beta1 "buf.build/gen/go/lekkodev/cli/protocolbuffers/go/lekko/bff/v1beta1"
featurev1beta1 "buf.build/gen/go/lekkodev/cli/protocolbuffers/go/lekko/feature/v1beta1"
Expand Down Expand Up @@ -69,27 +66,20 @@ func syncGoCmd() *cobra.Command {
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
b, err := os.ReadFile("go.mod")
if err != nil {
return errors.Wrap(err, "find go.mod in working directory")
}
mf, err := modfile.ParseLax("go.mod", b, nil)
if err != nil {
return err
}

var err error
if len(repoPath) == 0 {
repoPath, err = repo.PrepareGithubRepo()
if err != nil {
return err
}
}
f = args[0]
syncer, err := sync.NewGoSyncer(mf.Module.Mod.Path, f)
syncer := sync.NewGoSyncer()
repoContents, err := syncer.Sync(f)
if err != nil {
return errors.Wrap(err, "initialize code syncer")
return err
}
_, err = syncer.Sync(ctx, &repoPath)
err = sync.WriteContentsToLocalRepo(ctx, repoContents, repoPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -222,30 +212,24 @@ func isSame(ctx context.Context, existing map[string]map[string]*featurev1beta1.
if err != nil {
return false, err
}
b, err := os.ReadFile("go.mod")
if err != nil {
return false, err
}
mf, err := modfile.ParseLax("go.mod", b, nil)
if err != nil {
return false, err
}
dot := try.To1(dotlekko.ReadDotLekko(""))
nlProject := try.To1(native.DetectNativeLang(""))
files := try.To1(native.ListNativeConfigFiles(dot.LekkoPath, nlProject.Language))
var notEqual bool
var relPaths []string
for _, f := range files {
relativePath, err := filepath.Rel(wd, f)
relPath, err := filepath.Rel(wd, f)
if err != nil {
return false, err
}
//fmt.Printf("%s\n\n", mf.Module.Mod.Path)
g := sync.NewGoSyncerLite(mf.Module.Mod.Path, relativePath)
namespace, err := g.Sync(ctx, nil)
if err != nil {
return false, err
}
//fmt.Printf("%#v\n", namespace)
relPaths = append(relPaths, relPath)
}
g := sync.NewGoSyncer()
repoContents, err := g.Sync(relPaths...)
if err != nil {
return false, err
}
for _, namespace := range repoContents.Namespaces {
existingNs, ok := existing[namespace.Name]
if !ok {
// New namespace not in existing
Expand Down Expand Up @@ -485,7 +469,7 @@ func convertLangCmd() *cobra.Command {
}
fmt.Println(out)
} else {
privateFile := goToGo(ctx, f)
privateFile := goToGo(ctx, inputFile)
fmt.Println(privateFile)
}
return nil
Expand All @@ -497,34 +481,25 @@ func convertLangCmd() *cobra.Command {
return cmd
}

func goToGo(ctx context.Context, f []byte) string {
registry, err := prototypes.RegisterDynamicTypes(nil)
func goToGo(ctx context.Context, filePath string) string {
syncer := sync.NewGoSyncer()
repoContents, err := syncer.Sync(filePath)
if err != nil {
panic(err)
panic(errors.Wrap(err, "sync"))
}
err = registry.AddFileDescriptor(durationpb.File_google_protobuf_duration_proto, false)
if err != nil {
panic(err)
}
syncer := sync.NewGoSyncerLite("", "")
namespace, err := syncer.SourceToNamespace(ctx, f)
if err != nil {
panic(err)
if len(repoContents.Namespaces) != 1 {
panic("expected 1 namespace")
}
namespace := repoContents.Namespaces[0]
//fmt.Printf("%+v\n", namespace)
//fmt.Printf("%+v\n", registry.Types)
//fmt.Print("ON TO GENERATION\n")
// code gen based off that namespace object
g, err := gen.NewGoGenerator("", "/tmp", "", "", namespace.Name) // type registry?
if err != nil {
panic(err)
}
tr, err := syncer.GetTypeRegistry()
g.TypeRegistry = tr
g, err := gen.NewGoGenerator("", "/tmp", "", repoContents)
if err != nil {
panic(err)
}
_, privateFile, err := g.GenNamespaceFiles(ctx, namespace.Features, nil)
_, privateFile, err := g.GenNamespaceFiles(ctx, namespace.Name, namespace.Features, nil)
if err != nil {
panic(err)
}
Expand Down
143 changes: 74 additions & 69 deletions cmd/lekko/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ package main
import (
//"bytes"
"context"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"

//"os/exec"
"testing"

"github.com/lekkodev/cli/pkg/gen"
"github.com/lekkodev/cli/pkg/sync"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
//"google.golang.org/protobuf/types/descriptorpb"
)

Expand Down Expand Up @@ -95,61 +104,6 @@ func Test_writeProtoFiles(t *testing.T) {
}
*/

func Test_goToGo(t *testing.T) {
t.Run("simple", func(t *testing.T) {
ctx := context.Background()
f, err := os.ReadFile("./testdata/simple.go")
if err != nil {
panic(err)
}
if got := goToGo(ctx, f); got != string(f) {
diff, err := DiffStyleOutput(string(f), got)
if err != nil {
panic(err)
}
t.Errorf("Difference Found: %s\n", diff)
}
})
t.Run("withcontext", func(t *testing.T) {
ctx := context.Background()
f, err := os.ReadFile("./testdata/withcontext.go")
if err != nil {
panic(err)
}
if got := goToGo(ctx, f); got != string(f) {
t.Errorf("goToGo() = \n===\n%v+++, want \n===\n%v+++", got, string(f))
}
})

t.Run("one_two", func(t *testing.T) {
ctx := context.Background()
f, err := os.ReadFile("./testdata/twostructs.go")
if err != nil {
panic(err)
}
if got := goToGo(ctx, f); got != string(f) {
t.Errorf("goToGo() = \n===\n%v+++, want \n===\n%v+++", got, string(f))
}
})
}

func Test_Gertrude(t *testing.T) {
t.Run("gertrude", func(t *testing.T) {
ctx := context.Background()
f, err := os.ReadFile("./testdata/gertrude.go")
if err != nil {
panic(err)
}
if got := goToGo(ctx, f); got != string(f) {
diff, err := DiffStyleOutput(string(f), got)
if err != nil {
panic(err)
}
t.Errorf("Difference Found: %s\n", diff)
}
})
}

func DiffStyleOutput(a, b string) (string, error) {
// Create temporary files to hold the input strings
fileA, err := os.CreateTemp("", "fileA")
Expand Down Expand Up @@ -189,21 +143,72 @@ func DiffStyleOutput(a, b string) (string, error) {
return string(output), nil
}

func TestDefault(t *testing.T) {
t.Run("default", func(t *testing.T) {
ctx := context.Background()
f, err := os.ReadFile("./testdata/default.go")
if err != nil {
panic(err)
}
if got := goToGo(ctx, f); got != string(f) {
diff, err := DiffStyleOutput(string(f), got)
if err != nil {
panic(err)
}
t.Errorf("Difference Found: %s\n", diff)
// Test code -> repo -> code (compare) -> repo (compare)
func TestGoSyncToGenToSync(t *testing.T) {
Comment on lines +146 to +147
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This replaces the removed test methods by running through all of the test cases but also adding an extra sync step to compare parsed repository contents.

if err := filepath.WalkDir("./testdata", func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() && strings.HasSuffix(d.Name(), ".go") {
t.Run(strings.TrimSuffix(d.Name(), ".go"), func(t *testing.T) {
ctx := context.Background()
orig, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read test file %s: %v", path, err)
}

s1 := sync.NewGoSyncer()
r1, err := s1.Sync(path)
if err != nil {
t.Fatalf("sync 1: %v", err)
}

tmpd, err := os.MkdirTemp("", "test")
if err != nil {
t.Fatalf("tmp dir: %v", err)
}
defer os.RemoveAll(tmpd)

g, err := gen.NewGoGenerator("test", tmpd, "", r1)
if err != nil {
t.Fatalf("initialize gen: %v", err)
}
namespace := r1.Namespaces[0]
_, private, err := g.GenNamespaceFiles(ctx, namespace.Name, namespace.Features, nil)
if err != nil {
t.Fatalf("gen: %v", err)
}
privatePath := filepath.Join(tmpd, fmt.Sprintf("%s.go", namespace.Name))
if err := os.WriteFile(privatePath, []byte(private), 0600); err != nil {
t.Fatalf("write private %s: %v", privatePath, err)
}

if string(orig) != private {
diff, err := DiffStyleOutput(string(orig), private)
if err != nil {
t.Fatalf("diff: %v", err)
}
t.Fatalf("mismatch in generated code: %s", diff)
}

s2 := sync.NewGoSyncer()
r2, err := s2.Sync(privatePath)
if err != nil {
t.Fatalf("sync 2: %v", err)
}
// NOTE: Because Anys contained serialized values, their serialization needs to be
// deterministic for this check to always pass even if their deserialized values are equal.
// The problem is that the determinism is not canonical across languages.
// We should keep this in mind.
if !proto.Equal(r1, r2) {
r1json := protojson.Format(r1)
r2json := protojson.Format(r2)
diff, _ := DiffStyleOutput(r1json, r2json)
t.Fatalf("mismatch in repo contents: %s", diff)
}
})
}
})
return nil
}); err != nil {
t.Fatalf("walk: %v", err)
}
}

/*
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ replace github.com/bazelbuild/buildtools => github.com/lekkodev/buildtools v0.0.

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-20240801183157-5b3ff9a64103.2
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.34.2-20240923164736-6b09ba83efbf.2
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/atotto/clipboard v0.1.4
github.com/bazelbuild/buildtools v0.0.0-20220907133145-b9bfff5d7f91
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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-20240801183157-5b3ff9a64103.2 h1:ASguPgU4ltdoHHy+YTlpg79TDwhHpWBk3MKmxfbaE7Y=
buf.build/gen/go/lekkodev/cli/protocolbuffers/go v1.34.2-20240801183157-5b3ff9a64103.2/go.mod h1:j/ek65dWz+D5GM7p9QUiHQj5X5gtRUMfGl1+GpSfm6g=
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/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=
Expand Down
Loading