From e6778d65a621c10ac6796c366b44d9976d1bd32b Mon Sep 17 00:00:00 2001 From: Brad Moylan Date: Wed, 17 Nov 2021 07:11:47 -0800 Subject: [PATCH] improvement: Update conjure-go to 6.11.0 (#158) --- changelog/@unreleased/pr-158.v2.yml | 5 ++ go.mod | 2 +- go.sum | 4 +- integration_test/integration_test.go | 8 ++-- .../conjure/spec/structs.conjure.go | 4 +- .../conjure/spec/unions.conjure.go | 4 +- .../palantir/conjure-go/v6/conjure/conjure.go | 46 +++++++++---------- .../conjure-go/v6/conjure/enumwriter.go | 22 ++++----- .../palantir/conjure-go/v6/conjure/outputs.go | 1 - .../conjure-go/v6/conjure/snip/imports.go | 34 +++++++++++--- .../v6/conjure/types/conjure_definition.go | 29 ++++++++++++ vendor/modules.txt | 2 +- 12 files changed, 107 insertions(+), 54 deletions(-) create mode 100644 changelog/@unreleased/pr-158.v2.yml diff --git a/changelog/@unreleased/pr-158.v2.yml b/changelog/@unreleased/pr-158.v2.yml new file mode 100644 index 00000000..6e22a5bf --- /dev/null +++ b/changelog/@unreleased/pr-158.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Update conjure-go to 6.11.0 + links: + - https://github.com/palantir/godel-conjure-plugin/pull/158 diff --git a/go.mod b/go.mod index 11db8bd5..6cbb7093 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/go-bindata/go-bindata v3.1.2+incompatible github.com/mholt/archiver v2.1.0+incompatible github.com/nmiyake/pkg/dirs v1.1.0 - github.com/palantir/conjure-go/v6 v6.10.0 + github.com/palantir/conjure-go/v6 v6.11.0 github.com/palantir/distgo v1.29.0 github.com/palantir/go-ptimports/v2 v2.10.0 // indirect github.com/palantir/godel/v2 v2.41.0 diff --git a/go.sum b/go.sum index af328c42..46f682a6 100644 --- a/go.sum +++ b/go.sum @@ -324,8 +324,8 @@ github.com/palantir/conjure-go-runtime/v2 v2.23.0/go.mod h1:tDxL5svQhuafB7yHQbie github.com/palantir/conjure-go-runtime/v2 v2.25.0/go.mod h1:tDxL5svQhuafB7yHQbieBDsNwtRTHrQMXmlYrIkZttI= github.com/palantir/conjure-go/v4 v4.2.0/go.mod h1:VSVefkM94TdXRyP7wvBeURBNOFkYRJEtxPi8BeyKFi8= github.com/palantir/conjure-go/v5 v5.0.1/go.mod h1:qAWik7iYoJtlHwyJ/sG0FuJx1PtzrLVCUglLH1k7NrE= -github.com/palantir/conjure-go/v6 v6.10.0 h1:HVxuoEe+LNGfdXyoOrj/ZeqZOhNgpnZjhLZ1IBRhSfg= -github.com/palantir/conjure-go/v6 v6.10.0/go.mod h1:rAePFCXmBwV/p6r762CqAczU4x5XbJ3K7s2ahnK2R+Q= +github.com/palantir/conjure-go/v6 v6.11.0 h1:quWzA3kQ8EkyUuuaVdA2x1z8lw28S1bZc3DAb0o9HC0= +github.com/palantir/conjure-go/v6 v6.11.0/go.mod h1:rAePFCXmBwV/p6r762CqAczU4x5XbJ3K7s2ahnK2R+Q= github.com/palantir/distgo v1.2.0/go.mod h1:jOCcoLD92T/eN4VfRxczaDIWsqyy1f0yQZMFOt3cJCw= github.com/palantir/distgo v1.20.0/go.mod h1:Jfsd7o7+kzCY8a3xpbPabr5IMjW6zSNOKLTNa8TJH4g= github.com/palantir/distgo v1.29.0 h1:xWmSZKmFq362QzmPtzsQ3aPWFE1RfvxnkU7FQDsTHnQ= diff --git a/integration_test/integration_test.go b/integration_test/integration_test.go index 080cb0fc..c28dde1a 100644 --- a/integration_test/integration_test.go +++ b/integration_test/integration_test.go @@ -122,8 +122,8 @@ projects: package api import ( - safejson "github.com/palantir/pkg/safejson" - safeyaml "github.com/palantir/pkg/safeyaml" + "github.com/palantir/pkg/safejson" + "github.com/palantir/pkg/safeyaml" ) type TestCase struct { @@ -160,8 +160,8 @@ import ( "context" "fmt" - safejson "github.com/palantir/pkg/safejson" - safeyaml "github.com/palantir/pkg/safeyaml" + "github.com/palantir/pkg/safejson" + "github.com/palantir/pkg/safeyaml" ) type TestUnion struct { diff --git a/vendor/github.com/palantir/conjure-go/v6/conjure-api/conjure/spec/structs.conjure.go b/vendor/github.com/palantir/conjure-go/v6/conjure-api/conjure/spec/structs.conjure.go index 12b6f7b5..cc4f1348 100644 --- a/vendor/github.com/palantir/conjure-go/v6/conjure-api/conjure/spec/structs.conjure.go +++ b/vendor/github.com/palantir/conjure-go/v6/conjure-api/conjure/spec/structs.conjure.go @@ -3,8 +3,8 @@ package spec import ( - safejson "github.com/palantir/pkg/safejson" - safeyaml "github.com/palantir/pkg/safeyaml" + "github.com/palantir/pkg/safejson" + "github.com/palantir/pkg/safeyaml" ) type AliasDefinition struct { diff --git a/vendor/github.com/palantir/conjure-go/v6/conjure-api/conjure/spec/unions.conjure.go b/vendor/github.com/palantir/conjure-go/v6/conjure-api/conjure/spec/unions.conjure.go index 27f7b13e..3fd3943c 100644 --- a/vendor/github.com/palantir/conjure-go/v6/conjure-api/conjure/spec/unions.conjure.go +++ b/vendor/github.com/palantir/conjure-go/v6/conjure-api/conjure/spec/unions.conjure.go @@ -6,8 +6,8 @@ import ( "context" "fmt" - safejson "github.com/palantir/pkg/safejson" - safeyaml "github.com/palantir/pkg/safeyaml" + "github.com/palantir/pkg/safejson" + "github.com/palantir/pkg/safeyaml" ) type AuthType struct { diff --git a/vendor/github.com/palantir/conjure-go/v6/conjure/conjure.go b/vendor/github.com/palantir/conjure-go/v6/conjure/conjure.go index f8fc442f..9790060d 100644 --- a/vendor/github.com/palantir/conjure-go/v6/conjure/conjure.go +++ b/vendor/github.com/palantir/conjure-go/v6/conjure/conjure.go @@ -15,12 +15,12 @@ package conjure import ( - "path" "path/filepath" "sort" "github.com/dave/jennifer/jen" "github.com/palantir/conjure-go/v6/conjure-api/conjure/spec" + "github.com/palantir/conjure-go/v6/conjure/snip" "github.com/palantir/conjure-go/v6/conjure/types" "github.com/pkg/errors" ) @@ -47,54 +47,54 @@ func GenerateOutputFiles(conjureDefinition spec.ConjureDefinition, cfg OutputCon var files []*OutputFile for _, pkg := range def.Packages { if len(pkg.Aliases) > 0 { - aliasFile := newJenFile(pkg.ImportPath) + aliasFile := newJenFile(pkg, def) for _, alias := range pkg.Aliases { writeAliasType(aliasFile.Group, alias) } - files = append(files, newGoFile(filepath.Join(pkg.OutputDir, "aliases.conjure.go"), pkg.ImportPath, aliasFile)) + files = append(files, newGoFile(filepath.Join(pkg.OutputDir, "aliases.conjure.go"), aliasFile)) } if len(pkg.Enums) > 0 { - enumFile := newJenFile(pkg.ImportPath) + enumFile := newJenFile(pkg, def) for _, enum := range pkg.Enums { writeEnumType(enumFile.Group, enum) } - files = append(files, newGoFile(filepath.Join(pkg.OutputDir, "enums.conjure.go"), pkg.ImportPath, enumFile)) + files = append(files, newGoFile(filepath.Join(pkg.OutputDir, "enums.conjure.go"), enumFile)) } if len(pkg.Objects) > 0 { - objectFile := newJenFile(pkg.ImportPath) + objectFile := newJenFile(pkg, def) for _, object := range pkg.Objects { writeObjectType(objectFile.Group, object) } - files = append(files, newGoFile(filepath.Join(pkg.OutputDir, "structs.conjure.go"), pkg.ImportPath, objectFile)) + files = append(files, newGoFile(filepath.Join(pkg.OutputDir, "structs.conjure.go"), objectFile)) } if len(pkg.Unions) > 0 { - unionFile := newJenFile(pkg.ImportPath) + unionFile := newJenFile(pkg, def) for _, union := range pkg.Unions { writeUnionType(unionFile.Group, union, cfg.GenerateFuncsVisitor) } - files = append(files, newGoFile(filepath.Join(pkg.OutputDir, "unions.conjure.go"), pkg.ImportPath, unionFile)) + files = append(files, newGoFile(filepath.Join(pkg.OutputDir, "unions.conjure.go"), unionFile)) } if len(pkg.Errors) > 0 { - errorFile := newJenFile(pkg.ImportPath) + errorFile := newJenFile(pkg, def) for _, errorDef := range pkg.Errors { writeErrorType(errorFile.Group, errorDef) } astErrorInitFunc(errorFile.Group, pkg.Errors) - files = append(files, newGoFile(filepath.Join(pkg.OutputDir, "errors.conjure.go"), pkg.ImportPath, errorFile)) + files = append(files, newGoFile(filepath.Join(pkg.OutputDir, "errors.conjure.go"), errorFile)) } if len(pkg.Services) > 0 { - serviceFile := newJenFile(pkg.ImportPath) + serviceFile := newJenFile(pkg, def) for _, service := range pkg.Services { writeServiceType(serviceFile.Group, service) } - files = append(files, newGoFile(filepath.Join(pkg.OutputDir, "services.conjure.go"), pkg.ImportPath, serviceFile)) + files = append(files, newGoFile(filepath.Join(pkg.OutputDir, "services.conjure.go"), serviceFile)) } if len(pkg.Services) > 0 && cfg.GenerateServer { - serverFile := newJenFile(pkg.ImportPath) + serverFile := newJenFile(pkg, def) for _, server := range pkg.Services { writeServerType(serverFile.Group, server) } - files = append(files, newGoFile(filepath.Join(pkg.OutputDir, "servers.conjure.go"), pkg.ImportPath, serverFile)) + files = append(files, newGoFile(filepath.Join(pkg.OutputDir, "servers.conjure.go"), serverFile)) } } @@ -105,19 +105,17 @@ func GenerateOutputFiles(conjureDefinition spec.ConjureDefinition, cfg OutputCon return files, nil } -func newJenFile(importPath string) *jen.File { - f := jen.NewFilePath(importPath) - f.ImportNames(map[string]string{ - "github.com/palantir/witchcraft-go-params": "wparams", - "github.com/palantir/witchcraft-go-error": "werror", - }) +func newJenFile(pkg types.ConjurePackage, def *types.ConjureDefinition) *jen.File { + f := jen.NewFilePathName(pkg.ImportPath, pkg.PackageName) + f.ImportNames(snip.DefaultImportsToPackageNames) + for _, conjurePackage := range def.Packages { + f.ImportName(conjurePackage.ImportPath, conjurePackage.PackageName) + } return f } -func newGoFile(filePath, goImportPath string, file *jen.File) *OutputFile { - _, pkgName := path.Split(goImportPath) +func newGoFile(filePath string, file *jen.File) *OutputFile { return &OutputFile{ - pkgName: pkgName, absPath: filePath, file: file, } diff --git a/vendor/github.com/palantir/conjure-go/v6/conjure/enumwriter.go b/vendor/github.com/palantir/conjure-go/v6/conjure/enumwriter.go index e7846381..0ba7e1ec 100644 --- a/vendor/github.com/palantir/conjure-go/v6/conjure/enumwriter.go +++ b/vendor/github.com/palantir/conjure-go/v6/conjure/enumwriter.go @@ -81,17 +81,17 @@ func astForEnumIsUnknown(typeName string, values []*types.Field) *jen.Statement return jen.Commentf("IsUnknown returns false for all known variants of %s and true otherwise.", typeName). Line(). Func(). - Params(jen.Id(enumReceiverName).Id(typeName)).Id("IsUnknown").Params().Params(jen.Bool()).Block( - jen.Switch(jen.Id(enumReceiverName).Dot(enumStructFieldName)).Block( - jen.CaseFunc(func(conds *jen.Group) { - for _, valDef := range values { - conds.Id(typeName + "_" + valDef.Name) - } - }). - Block(jen.Return(jen.False())), - ), - jen.Return(jen.True()), - ) + Params(jen.Id(enumReceiverName).Id(typeName)).Id("IsUnknown").Params().Params(jen.Bool()).BlockFunc(func(methodBody *jen.Group) { + if len(values) > 0 { + methodBody.Switch(jen.Id(enumReceiverName).Dot(enumStructFieldName)).Block( + jen.CaseFunc(func(conds *jen.Group) { + for _, valDef := range values { + conds.Id(typeName + "_" + valDef.Name) + } + }).Block(jen.Return(jen.False()))) + } + methodBody.Return(jen.True()) + }) } func astForEnumValueMethod(typeName string) *jen.Statement { diff --git a/vendor/github.com/palantir/conjure-go/v6/conjure/outputs.go b/vendor/github.com/palantir/conjure-go/v6/conjure/outputs.go index 3f92f722..adcf0ced 100644 --- a/vendor/github.com/palantir/conjure-go/v6/conjure/outputs.go +++ b/vendor/github.com/palantir/conjure-go/v6/conjure/outputs.go @@ -28,7 +28,6 @@ import ( type OutputFile struct { absPath string - pkgName string file *jen.File } diff --git a/vendor/github.com/palantir/conjure-go/v6/conjure/snip/imports.go b/vendor/github.com/palantir/conjure-go/v6/conjure/snip/imports.go index c45ef98c..1b7aa358 100644 --- a/vendor/github.com/palantir/conjure-go/v6/conjure/snip/imports.go +++ b/vendor/github.com/palantir/conjure-go/v6/conjure/snip/imports.go @@ -21,8 +21,30 @@ import ( const ( pal = "github.com/palantir/" cgr = pal + "conjure-go-runtime/v2/" + wgs = pal + "witchcraft-go-server/v2/" ) +var DefaultImportsToPackageNames = map[string]string{ + cgr + "conjure-go-client/httpclient": "httpclient", + cgr + "conjure-go-contract/codecs": "codecs", + cgr + "conjure-go-contract/errors": "errors", + cgr + "conjure-go-server/httpserver": "httpserver", + pal + "pkg/binary": "binary", + pal + "pkg/bearertoken": "bearertoken", + pal + "pkg/boolean": "boolean", + pal + "pkg/datetime": "datetime", + pal + "pkg/rid": "rid", + pal + "pkg/safelong": "safelong", + pal + "pkg/safejson": "safejson", + pal + "pkg/safeyaml": "safeyaml", + pal + "pkg/uuid": "uuid", + pal + "witchcraft-go-error": "werror", + pal + "witchcraft-go-params": "wparams", + wgs + "witchcraft/wresource": "wresource", + wgs + "wrouter": "wrouter", + "github.com/tidwall/gjson": "gjson", +} + // A set of imported references included in generated code. // Each entry is a func() *jen.Statement, typically the Clone method. // This ensures there are no side effects caused by mutating the global variables. @@ -135,12 +157,12 @@ var ( WerrorWrap = jen.Qual(pal+"witchcraft-go-error", "Wrap").Clone WerrorWrapContext = jen.Qual(pal+"witchcraft-go-error", "WrapWithContextParams").Clone - WresourceNew = jen.Qual(pal+"witchcraft-go-server/v2/witchcraft/wresource", "New").Clone - WrouterPathParams = jen.Qual(pal+"witchcraft-go-server/v2/wrouter", "PathParams").Clone - WrouterRouter = jen.Qual(pal+"witchcraft-go-server/v2/wrouter", "Router").Clone - WrouterSafeHeaderParams = jen.Qual(pal+"witchcraft-go-server/v2/wrouter", "SafeHeaderParams").Clone - WrouterSafePathParams = jen.Qual(pal+"witchcraft-go-server/v2/wrouter", "SafePathParams").Clone - WrouterSafeQueryParams = jen.Qual(pal+"witchcraft-go-server/v2/wrouter", "SafeQueryParams").Clone + WresourceNew = jen.Qual(wgs+"witchcraft/wresource", "New").Clone + WrouterPathParams = jen.Qual(wgs+"wrouter", "PathParams").Clone + WrouterRouter = jen.Qual(wgs+"wrouter", "Router").Clone + WrouterSafeHeaderParams = jen.Qual(wgs+"wrouter", "SafeHeaderParams").Clone + WrouterSafePathParams = jen.Qual(wgs+"wrouter", "SafePathParams").Clone + WrouterSafeQueryParams = jen.Qual(wgs+"wrouter", "SafeQueryParams").Clone GJSONNull = jen.Qual("github.com/tidwall/gjson", "Null").Clone GJSONFalse = jen.Qual("github.com/tidwall/gjson", "False").Clone diff --git a/vendor/github.com/palantir/conjure-go/v6/conjure/types/conjure_definition.go b/vendor/github.com/palantir/conjure-go/v6/conjure/types/conjure_definition.go index 053c4bb0..244be093 100644 --- a/vendor/github.com/palantir/conjure-go/v6/conjure/types/conjure_definition.go +++ b/vendor/github.com/palantir/conjure-go/v6/conjure/types/conjure_definition.go @@ -16,6 +16,10 @@ package types import ( "fmt" + "path" + "regexp" + "strings" + "unicode" "github.com/dave/jennifer/jen" "github.com/palantir/conjure-go/v6/conjure-api/conjure/spec" @@ -33,6 +37,7 @@ type ConjurePackage struct { ConjurePackage string ImportPath string OutputDir string + PackageName string Aliases []*AliasType Enums []*EnumType @@ -167,6 +172,7 @@ func NewConjureDefinition(outputBaseDir string, def spec.ConjureDefinition) (*Co pkg.ConjurePackage = pkgName pkg.ImportPath = paths.conjurePkgToGoPkg(pkgName) pkg.OutputDir = paths.conjurePkgToFilePath(pkgName) + pkg.PackageName = sanitizePackageName(pkg.ImportPath) packages[pkgName] = pkg } return &ConjureDefinition{ @@ -488,3 +494,26 @@ func (unresolvedReferencePlaceholder) Code() *jen.Statement { func (unresolvedReferencePlaceholder) String() string { panic("unresolvedReferencePlaceholder does not implement methods") } + +// sanitizePackageName is based on `guessAlias` from jen/file.go: https://github.com/dave/jennifer/blob/45cc0b7eb71a469771aa486323bc53189030b60e/jen/file.go#L224 +// It uses path.Base then removes non-alphanumerics and leading digits. +func sanitizePackageName(importPath string) string { + alias := path.Base(importPath) + + // alias should be lower case + alias = strings.ToLower(alias) + + // alias should now only contain alphanumerics + importsRegex := regexp.MustCompile(`[^a-z0-9]`) + alias = importsRegex.ReplaceAllString(alias, "") + + // can't have a first digit, per Go identifier rules, so just skip them + alias = strings.TrimLeftFunc(alias, unicode.IsDigit) + + // If path part was all digits, we may be left with an empty string. In this case use "pkg" as the alias. + if alias == "" { + alias = "pkg" + } + + return alias +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 989b41e9..e8b553e3 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -55,7 +55,7 @@ github.com/nmiyake/pkg/dirs github.com/nmiyake/pkg/errorstringer # github.com/nwaples/rardecode v1.0.0 github.com/nwaples/rardecode -# github.com/palantir/conjure-go/v6 v6.10.0 +# github.com/palantir/conjure-go/v6 v6.11.0 ## explicit github.com/palantir/conjure-go/v6/conjure github.com/palantir/conjure-go/v6/conjure-api/conjure/spec