Skip to content

Commit

Permalink
Use subset of Go module identity for package name (#7868)
Browse files Browse the repository at this point in the history
* Use subset of Go module identity for package name
  • Loading branch information
jhendrixMSFT authored Apr 16, 2024
1 parent 963a492 commit de61b0c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 37 deletions.
2 changes: 1 addition & 1 deletion eng/pipelines/templates/steps/sdk-testgen-set-env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ steps:
displayName: "Install Node.js"
- task: GoTool@0
inputs:
version: '1.16.7'
version: '1.22.1'
displayName: "Install Go"
- script: |
npm install -g "@microsoft/rush"
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet/APIView/apiview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ variables:
BuildConfiguration: 'Release'
TypeScriptGeneratorDirectory: 'tools/apiview/parsers/js-api-parser'
GoParserPackagePath: 'src/go'
GoVersion: '1.18'
GoVersion: '1.22.1'
NugetSecurityAnalysisWarningLevel: 'none'
AzuriteConnectionString: "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1"
CosmosEmulatorConnectionString: "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
Expand Down
8 changes: 8 additions & 0 deletions src/go/cmd/api_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,11 @@ func TestVars(t *testing.T) {
require.EqualValues(t, 2, countSomeChoice)
require.True(t, hasHTTPClient)
}

func Test_getPackageNameFromModPath(t *testing.T) {
require.EqualValues(t, "foo", getPackageNameFromModPath("foo"))
require.EqualValues(t, "foo", getPackageNameFromModPath("foo/v2"))
require.EqualValues(t, "sdk/foo", getPackageNameFromModPath("github.com/Azure/azure-sdk-for-go/sdk/foo"))
require.EqualValues(t, "sdk/foo/bar", getPackageNameFromModPath("github.com/Azure/azure-sdk-for-go/sdk/foo/bar"))
require.EqualValues(t, "sdk/foo/bar", getPackageNameFromModPath("github.com/Azure/azure-sdk-for-go/sdk/foo/bar/v5"))
}
41 changes: 20 additions & 21 deletions src/go/cmd/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ type Module struct {
packages map[string]*Pkg
}

var majorVerSuffix = regexp.MustCompile(`/v\d+$`)

// fetches the value for Module.PackageName from the full module path
func getPackageNameFromModPath(modPath string) string {
// for official SDKs, use a subset of the full module path
// e.g. github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appcomplianceautomation/armappcomplianceautomation
// becomes sdk/resourcemanager/appcomplianceautomation/armappcomplianceautomation
if suffix, ok := strings.CutPrefix(modPath, "github.com/Azure/azure-sdk-for-go/"); ok {
modPath = suffix
}
// now strip off any major version suffix
if loc := majorVerSuffix.FindStringIndex(modPath); loc != nil {
modPath = modPath[:loc[0]]
}
return modPath
}

// NewModule indexes an Azure SDK module's ASTs
func NewModule(dir string) (*Module, error) {
mf, err := parseModFile(dir)
Expand All @@ -48,30 +65,12 @@ func NewModule(dir string) (*Module, error) {
// sdkRoot is the path on disk to the sdk folder e.g. /home/user/me/azure-sdk-for-go/sdk.
// Used to find definitions of types imported from other Azure SDK modules.
sdkRoot := ""
packageName := ""
if before, after, found := strings.Cut(dir, fmt.Sprintf("%s%c", sdkDirName, filepath.Separator)); found {
if before, _, found := strings.Cut(dir, fmt.Sprintf("%s%c", sdkDirName, filepath.Separator)); found {
sdkRoot = filepath.Join(before, sdkDirName)
if filepath.Base(after) == "internal" {
packageName = after
} else {
packageName = filepath.Base(after)
}
fmt.Printf("Package Name: %s\n", packageName)
}

//Package name can still be empty when generating API review using uploaded zip folder of a specific package in which case parent directory will not be sdk
if packageName == "" {
modulePath := mf.Module.Mod.Path
packageName = path.Base(modulePath)
fmt.Printf("Module path: %s\n", modulePath)
// Set relative path as package name for internal package to avoid collision
if packageName == "internal" {
if _, after, found := strings.Cut(modulePath, fmt.Sprintf("/%s/", sdkDirName)); found {
packageName = after
}
}
fmt.Printf("Package Name: %s\n", packageName)
}
packageName := getPackageNameFromModPath(mf.Module.Mod.Path)
fmt.Printf("Package Name: %s\n", packageName)
m := Module{Name: filepath.Base(dir), PackageName: packageName, packages: map[string]*Pkg{}}

baseImportPath := path.Dir(mf.Module.Mod.Path) + "/"
Expand Down
8 changes: 4 additions & 4 deletions src/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ go 1.18

require (
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.7.0
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
golang.org/x/mod v0.14.0
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
golang.org/x/mod v0.16.0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
18 changes: 8 additions & 10 deletions src/go/go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -10,15 +10,13 @@ github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit de61b0c

Please sign in to comment.