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

feat!: Service per dir #6

Merged
merged 3 commits into from
Jun 12, 2023
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
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ go 1.19

require (
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appconfiguration/armappconfiguration/v2 v2.0.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armmanagedapplications v1.1.1
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/solutions/armmanagedapplications v1.1.1
github.com/google/go-cmp v0.5.9
github.com/jpillora/longestcommon v0.0.0-20161227235612-adb9d91ee629
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30
)

Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ github.com/Azure/azure-sdk-for-go/sdk/internal v1.2.0 h1:leh5DwKv6Ihwi+h60uHtn6U
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/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armmanagedapplications v1.1.1 h1:/tJ5Wpv4WJMWhrvQ4efhm+DlBPdohOSSOWaJkp4QjWw=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armmanagedapplications v1.1.1/go.mod h1:8Qc+OxIwt9ev8/CfRUC2/cchDPdHzjYFajZiDbiXxDU=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/solutions/armmanagedapplications v1.1.1 h1:hioFVSo4plF3RHTlGiTW9NSwdS8lN34yRT1QDpTgDsw=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/solutions/armmanagedapplications v1.1.1/go.mod h1:AA8RASHl5shSrLLA0/IpBbLNsoSJkjIm68X9EGDQRgo=
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/jpillora/longestcommon v0.0.0-20161227235612-adb9d91ee629 h1:1dSBUfGlorLAua2CRx0zFN7kQsTpE2DQSmr7rrTNgY8=
github.com/jpillora/longestcommon v0.0.0-20161227235612-adb9d91ee629/go.mod h1:mb5nS4uRANwOJSZj8rlCWAfAcGi72GGMIXx+xGOjA7M=
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=
Expand Down
48 changes: 35 additions & 13 deletions interfaces/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"strings"
"text/template"

"github.com/jpillora/longestcommon"

"golang.org/x/exp/maps"
)

Expand Down Expand Up @@ -51,16 +53,41 @@ func WithExtraImports(f func(reflect.Method) []string) Option {
}
}

func getPackageNames(clientInfos []clientInfo) []string {
versionPattern := regexp.MustCompile(`/v\d+$`)
allImports := make([]string, len(clientInfos))
for i, clientInfo := range clientInfos {
allImports[i] = clientInfo.Import
}
// To get the shortest possible package name without collisions, we need to find the longest common prefix
importPrefix := longestcommon.Prefix(allImports)
packageNames := make([]string, len(clientInfos))
for i, clientInfo := range clientInfos {
var pkgName string
if clientInfo.Import == importPrefix {
pkgName = versionPattern.ReplaceAllString(clientInfo.Import, "")
pkgName = path.Base(pkgName)
} else {
pkgName = strings.TrimPrefix(clientInfo.Import, importPrefix)
pkgName = strings.ReplaceAll(versionPattern.ReplaceAllString(pkgName, ""), "/", "_")
}

packageNames[i] = strings.ReplaceAll(pkgName, "-", "")
}
return packageNames
}

func getTemplateDataFromClientInfos(clientInfos []clientInfo) []serviceTemplateData {
packageNames := getPackageNames(clientInfos)
services := make([]serviceTemplateData, 0)
serviceMap := make(map[string][]clientInfo)
for _, clientInfo := range clientInfos {
serviceMap[clientInfo.PackageName] = append(serviceMap[clientInfo.PackageName], clientInfo)
for i, clientInfo := range clientInfos {
serviceMap[packageNames[i]] = append(serviceMap[packageNames[i]], clientInfo)
}
for packageName, clientInfos := range serviceMap {
for packageName, infos := range serviceMap {
imports := make(map[string]bool)
clientsTemplateData := make([]clientTemplateData, 0)
for _, clientInfo := range clientInfos {
for _, clientInfo := range infos {
imports[clientInfo.Import] = true
for _, extraImport := range clientInfo.ExtraImports {
imports[extraImport] = true
Expand Down Expand Up @@ -105,7 +132,7 @@ func Generate(clients []any, dir string, opts ...Option) error {
if err := serviceTpl.Execute(&buff, service); err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}
filePath := path.Join(dir, fmt.Sprintf("%s.go", service.PackageName))
filePath := path.Join(dir, service.PackageName, fmt.Sprintf("%s.go", service.PackageName))
err := formatAndWriteFile(filePath, buff)
if err != nil {
return fmt.Errorf("failed to format and write file for service %v: %w", service, err)
Expand Down Expand Up @@ -176,7 +203,6 @@ func signature(name string, f any) string {

type clientInfo struct {
Import string
PackageName string
ClientName string
Signatures []string
ExtraImports []string
Expand All @@ -197,12 +223,6 @@ func getClientInfo(client any, opts *Options) clientInfo {
v := reflect.ValueOf(client)
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)
Expand All @@ -216,7 +236,6 @@ func getClientInfo(client any, opts *Options) clientInfo {
}
return clientInfo{
Import: pkgPath,
PackageName: pkgName,
ClientName: clientName,
Signatures: signatures,
ExtraImports: extraImports,
Expand All @@ -231,6 +250,9 @@ func formatAndWriteFile(filePath string, buff bytes.Buffer) error {
} else {
content = formattedContent
}
if err := os.MkdirAll(path.Dir(filePath), 0755); err != nil {
return fmt.Errorf("failed to create directory %s: %w", path.Dir(filePath), err)
}
if err := os.WriteFile(filePath, content, 0644); err != nil {
return fmt.Errorf("failed to write file %s: %w", filePath, err)
}
Expand Down
84 changes: 78 additions & 6 deletions interfaces/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appconfiguration/armappconfiguration/v2"
resourcesarmmanagedapplications "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armmanagedapplications"
Copy link
Member Author

@erezrokah erezrokah Jun 12, 2023

Choose a reason for hiding this comment

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

Before this PR, we were taking the last part of the import part, which means resources/armmanagedapplications and solutions/armmanagedapplications overlapped

solutionsarmmanagedapplications "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/solutions/armmanagedapplications"
"github.com/google/go-cmp/cmp"
)

Expand Down Expand Up @@ -36,7 +38,7 @@ func (*Client) NewPager(_ context.Context) *Pager[Response] {
}

var wantOutput = `// Code generated by codegen; DO NOT EDIT.
package services
package interfaces

import (
"github.com/cloudquery/codegen/interfaces"
Expand All @@ -57,7 +59,7 @@ func (*Client2) ListTables(_ context.Context) error {
}

var wantOutputMultipleClients = `// Code generated by codegen; DO NOT EDIT.
package services
package interfaces

import (
"github.com/cloudquery/codegen/interfaces"
Expand All @@ -77,7 +79,7 @@ type Client2 interface {
`

var wantOutputNonV1 = `// Code generated by codegen; DO NOT EDIT.
package services
package armappconfiguration

import (
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appconfiguration/armappconfiguration/v2"
Expand All @@ -89,6 +91,44 @@ type ConfigurationStoresClient interface {
}
`

var wantOutputOverlappingPackages = map[string]string{
"solutions_armmanagedapplications/solutions_armmanagedapplications.go": `// Code generated by codegen; DO NOT EDIT.
package solutions_armmanagedapplications

import (
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/solutions/armmanagedapplications"
)

//go:generate mockgen -package=mocks -destination=../mocks/solutions_armmanagedapplications.go -source=solutions_armmanagedapplications.go ApplicationClient
type ApplicationClient interface {
}
`,

"resources_armmanagedapplications/resources_armmanagedapplications.go": `// Code generated by codegen; DO NOT EDIT.
package resources_armmanagedapplications

import (
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armmanagedapplications"
)

//go:generate mockgen -package=mocks -destination=../mocks/resources_armmanagedapplications.go -source=resources_armmanagedapplications.go ApplicationClient
type ApplicationClient interface {
}
`,
"appconfiguration_armappconfiguration/appconfiguration_armappconfiguration.go": `// Code generated by codegen; DO NOT EDIT.
package appconfiguration_armappconfiguration

import (
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appconfiguration/armappconfiguration/v2"
)

//go:generate mockgen -package=mocks -destination=../mocks/appconfiguration_armappconfiguration.go -source=appconfiguration_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,
Expand All @@ -99,7 +139,7 @@ func TestGenerate(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
fileName := path.Join(dir, "interfaces.go")
fileName := path.Join(dir, "interfaces", "interfaces.go")
f, err := os.Open(fileName)
if err != nil {
t.Fatalf("failed to open file: %v", err)
Expand All @@ -124,7 +164,7 @@ func TestGenerateMultipleClientsSamePackage(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
fileName := path.Join(dir, "interfaces.go")
fileName := path.Join(dir, "interfaces", "interfaces.go")
f, err := os.Open(fileName)
if err != nil {
t.Fatalf("failed to open file: %v", err)
Expand All @@ -149,7 +189,7 @@ func TestGenerateNonV1(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
fileName := path.Join(dir, "armappconfiguration.go")
fileName := path.Join(dir, "armappconfiguration", "armappconfiguration.go")
f, err := os.Open(fileName)
if err != nil {
t.Fatalf("failed to open file: %v", err)
Expand All @@ -163,3 +203,35 @@ func TestGenerateNonV1(t *testing.T) {
t.Errorf("unexpected diff (-got +want):\n%s", diff)
}
}

func TestGenerateOverlappingPackageNames(t *testing.T) {
dir := t.TempDir()
err := Generate([]any{
&armappconfiguration.ConfigurationStoresClient{},
&resourcesarmmanagedapplications.ApplicationClient{},
&solutionsarmmanagedapplications.ApplicationClient{},
}, dir,
WithIncludeFunc(func(m reflect.Method) bool {
return MethodHasAnyPrefix(m, []string{"NewListPager"})
}),
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

for filename, want := range wantOutputOverlappingPackages {
fileName := path.Join(dir, filename)
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), want); diff != "" {
t.Errorf("unexpected diff (-got +want):\n%s", diff)
}
}
}
2 changes: 1 addition & 1 deletion interfaces/templates/service.go.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Code generated by codegen; DO NOT EDIT.
package services
package {{.PackageName}}

import (
{{- range .Imports }}
Expand Down