From df193c6f8a9ed2159a376a25cd81b6d2af416e26 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Fri, 9 Jun 2023 16:29:17 +0200 Subject: [PATCH] fix: Support packages versions greater than v1 --- go.mod | 8 ++++++++ go.sum | 22 +++++++++++++++++++++ interfaces/generate.go | 12 +++++++++++- interfaces/generate_test.go | 39 +++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index b6e934f..2a27c2a 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,14 @@ module github.com/cloudquery/codegen go 1.19 require ( + github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appconfiguration/armappconfiguration/v2 v2.0.0 github.com/google/go-cmp v0.5.9 golang.org/x/exp v0.0.0-20221230185412-738e83a70c30 ) + +require ( + github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0 // indirect + github.com/Azure/azure-sdk-for-go/sdk/internal v1.2.0 // indirect + golang.org/x/net v0.7.0 // indirect + golang.org/x/text v0.7.0 // indirect +) diff --git a/go.sum b/go.sum index a34501a..d05c303 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,26 @@ +github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0 h1:rTnT/Jrcm+figWlYz4Ixzt0SJVR2cMC8lvZcimipiEY= +github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0/go.mod h1:ON4tFdPTwRcgWEaVDrN3584Ef+b7GgSJaXxe5fW9t4M= +github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.2 h1:uqM+VoHjVH6zdlkLF2b6O0ZANcHoj3rO0PoQ3jglUJA= +github.com/Azure/azure-sdk-for-go/sdk/internal v1.2.0 h1:leh5DwKv6Ihwi+h60uHtn6UWAxBbZ0q8DwQVMzf61zw= +github.com/Azure/azure-sdk-for-go/sdk/internal v1.2.0/go.mod h1:eWRD7oawr1Mu1sLCawqVc0CUiF43ia3qQMxLscsKQ9w= +github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appconfiguration/armappconfiguration/v2 v2.0.0 h1:PWMXyUvMZxxu+7nMMEQUvhkChyyL5QHZS09l0pFFNy0= +github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appconfiguration/armappconfiguration/v2 v2.0.0/go.mod h1:FYPwnyAC+Ym6BnAdb/9EEMut5U8mI/vxIP9lqcMIUiQ= +github.com/AzureAD/microsoft-authentication-library-for-go v0.9.0 h1:UE9n9rkJF62ArLb1F3DEjRt8O3jLwMWdSoypKV4f3MU= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc= golang.org/x/exp v0.0.0-20221230185412-738e83a70c30 h1:m9O6OTJ627iFnN2JIWfdqlZCzneRO6EEBsHXI25P8ws= golang.org/x/exp v0.0.0-20221230185412-738e83a70c30/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= +golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= diff --git a/interfaces/generate.go b/interfaces/generate.go index 2ef2d4d..b87a92b 100644 --- a/interfaces/generate.go +++ b/interfaces/generate.go @@ -125,7 +125,13 @@ func normalizedGenericTypeName(str string) string { typeName := groups[1] normalizedGenericTypeName := strings.Split(typeName, "/") - return pattern.ReplaceAllString(str, "["+normalizedGenericTypeName[len(normalizedGenericTypeName)-1]+"]") + importName := normalizedGenericTypeName[len(normalizedGenericTypeName)-1] + versionPattern := regexp.MustCompile(`/v\d+\.`) + if versionPattern.MatchString(typeName) { + // Example typeName: github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appconfiguration/armappconfiguration/v2.ConfigurationStoresClientCreateResponse + importName = normalizedGenericTypeName[len(normalizedGenericTypeName)-2] + "." + strings.Split(normalizedGenericTypeName[len(normalizedGenericTypeName)-1], ".")[1] + } + return pattern.ReplaceAllString(str, "["+importName+"]") } // Adapted from https://stackoverflow.com/a/54129236 @@ -192,7 +198,11 @@ func getClientInfo(client any, opts *Options) clientInfo { t := v.Type() pkgPath := t.Elem().PkgPath() parts := strings.Split(pkgPath, "/") + versionPattern := regexp.MustCompile(`/v\d+$`) pkgName := parts[len(parts)-1] + if versionPattern.MatchString(pkgPath) { + pkgName = parts[len(parts)-2] + } clientName := t.Elem().Name() signatures := make([]string, 0) extraImports := make([]string, 0) diff --git a/interfaces/generate_test.go b/interfaces/generate_test.go index 3bcdf11..4d7fccb 100644 --- a/interfaces/generate_test.go +++ b/interfaces/generate_test.go @@ -8,6 +8,7 @@ import ( "reflect" "testing" + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appconfiguration/armappconfiguration/v2" "github.com/google/go-cmp/cmp" ) @@ -75,6 +76,19 @@ type Client2 interface { } ` +var wantOutputNonV1 = `// Code generated by codegen; DO NOT EDIT. +package services + +import ( + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appconfiguration/armappconfiguration/v2" +) + +//go:generate mockgen -package=mocks -destination=../mocks/armappconfiguration.go -source=armappconfiguration.go ConfigurationStoresClient +type ConfigurationStoresClient interface { + NewListPager(*armappconfiguration.ConfigurationStoresClientListOptions) *runtime.Pager[armappconfiguration.ConfigurationStoresClientListResponse] +} +` + func TestGenerate(t *testing.T) { dir := t.TempDir() err := Generate([]any{&Client{}}, dir, @@ -124,3 +138,28 @@ func TestGenerateMultipleClientsSamePackage(t *testing.T) { t.Errorf("unexpected diff (-got +want):\n%s", diff) } } + +func TestGenerateNonV1(t *testing.T) { + dir := t.TempDir() + err := Generate([]any{&armappconfiguration.ConfigurationStoresClient{}}, dir, + WithIncludeFunc(func(m reflect.Method) bool { + return MethodHasAnyPrefix(m, []string{"NewListPager"}) + }), + ) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + fileName := path.Join(dir, "armappconfiguration.go") + f, err := os.Open(fileName) + if err != nil { + t.Fatalf("failed to open file: %v", err) + } + defer f.Close() + b, err := io.ReadAll(f) + if err != nil { + t.Fatalf("failed to read file: %v", err) + } + if diff := cmp.Diff(string(b), wantOutputNonV1); diff != "" { + t.Errorf("unexpected diff (-got +want):\n%s", diff) + } +}