Skip to content

Commit

Permalink
fix: Force import alias when package ends in /v[0-9]+$ (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoylan authored Feb 1, 2023
1 parent 30ee143 commit 201c9c7
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 22 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-409.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Force import alias when package ends in /v[0-9]+$
links:
- https://github.com/palantir/conjure-go/pull/409
11 changes: 10 additions & 1 deletion conjure/conjure.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package conjure

import (
"path/filepath"
"regexp"
"sort"

"github.com/dave/jennifer/jen"
Expand Down Expand Up @@ -118,7 +119,11 @@ func newJenFile(pkg types.ConjurePackage, def *types.ConjureDefinition) *jen.Fil
f := jen.NewFilePathName(pkg.ImportPath, pkg.PackageName)
f.ImportNames(snip.DefaultImportsToPackageNames)
for _, conjurePackage := range def.Packages {
f.ImportName(conjurePackage.ImportPath, conjurePackage.PackageName)
if packageSuffixRequiresAlias(conjurePackage.ImportPath) {
f.ImportAlias(conjurePackage.ImportPath, conjurePackage.PackageName)
} else {
f.ImportName(conjurePackage.ImportPath, conjurePackage.PackageName)
}
}
return f
}
Expand All @@ -129,3 +134,7 @@ func newGoFile(filePath string, file *jen.File) *OutputFile {
file: file,
}
}

func packageSuffixRequiresAlias(importPath string) bool {
return regexp.MustCompile(`/v[0-9]+$`).MatchString(importPath)
}
10 changes: 10 additions & 0 deletions integration_test/testgenerated/imports/imports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ types:
package: com.palantir.pkg2.api
fields:
data: Struct1
ObjectInPackageEndingInVersion:
package: com.palantir.pkg4.v2
fields:
name: string
DifferentPackageEndingInVersion:
package: com.palantir.pkg5.v2
fields:
name: string
Union:
package: com.palantir.pkg3.api
union:
one: Struct1
two: Struct2
three: ObjectInPackageEndingInVersion
four: DifferentPackageEndingInVersion
13 changes: 0 additions & 13 deletions integration_test/testgenerated/imports/imports_test.go

This file was deleted.

64 changes: 56 additions & 8 deletions integration_test/testgenerated/imports/pkg3/api/unions.conjure.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@ import (

"github.com/palantir/conjure-go/v6/integration_test/testgenerated/imports/pkg1/api"
api1 "github.com/palantir/conjure-go/v6/integration_test/testgenerated/imports/pkg2/api"
v2 "github.com/palantir/conjure-go/v6/integration_test/testgenerated/imports/pkg4/v2"
v21 "github.com/palantir/conjure-go/v6/integration_test/testgenerated/imports/pkg5/v2"
"github.com/palantir/pkg/safejson"
"github.com/palantir/pkg/safeyaml"
)

type Union struct {
typ string
one *api.Struct1
two *api1.Struct2
typ string
one *api.Struct1
two *api1.Struct2
three *v2.ObjectInPackageEndingInVersion
four *v21.DifferentPackageEndingInVersion
}

type unionDeserializer struct {
Type string `json:"type"`
One *api.Struct1 `json:"one"`
Two *api1.Struct2 `json:"two"`
Type string `json:"type"`
One *api.Struct1 `json:"one"`
Two *api1.Struct2 `json:"two"`
Three *v2.ObjectInPackageEndingInVersion `json:"three"`
Four *v21.DifferentPackageEndingInVersion `json:"four"`
}

func (u *unionDeserializer) toStruct() Union {
return Union{typ: u.Type, one: u.One, two: u.Two}
return Union{typ: u.Type, one: u.One, two: u.Two, three: u.Three, four: u.Four}
}

func (u *Union) toSerializer() (interface{}, error) {
Expand All @@ -42,6 +48,16 @@ func (u *Union) toSerializer() (interface{}, error) {
Type string `json:"type"`
Two api1.Struct2 `json:"two"`
}{Type: "two", Two: *u.two}, nil
case "three":
return struct {
Type string `json:"type"`
Three v2.ObjectInPackageEndingInVersion `json:"three"`
}{Type: "three", Three: *u.three}, nil
case "four":
return struct {
Type string `json:"type"`
Four v21.DifferentPackageEndingInVersion `json:"four"`
}{Type: "four", Four: *u.four}, nil
}
}

Expand Down Expand Up @@ -78,7 +94,7 @@ func (u *Union) UnmarshalYAML(unmarshal func(interface{}) error) error {
return safejson.Unmarshal(jsonBytes, *&u)
}

func (u *Union) AcceptFuncs(oneFunc func(api.Struct1) error, twoFunc func(api1.Struct2) error, unknownFunc func(string) error) error {
func (u *Union) AcceptFuncs(oneFunc func(api.Struct1) error, twoFunc func(api1.Struct2) error, threeFunc func(v2.ObjectInPackageEndingInVersion) error, fourFunc func(v21.DifferentPackageEndingInVersion) error, unknownFunc func(string) error) error {
switch u.typ {
default:
if u.typ == "" {
Expand All @@ -89,6 +105,10 @@ func (u *Union) AcceptFuncs(oneFunc func(api.Struct1) error, twoFunc func(api1.S
return oneFunc(*u.one)
case "two":
return twoFunc(*u.two)
case "three":
return threeFunc(*u.three)
case "four":
return fourFunc(*u.four)
}
}

Expand All @@ -100,6 +120,14 @@ func (u *Union) TwoNoopSuccess(api1.Struct2) error {
return nil
}

func (u *Union) ThreeNoopSuccess(v2.ObjectInPackageEndingInVersion) error {
return nil
}

func (u *Union) FourNoopSuccess(v21.DifferentPackageEndingInVersion) error {
return nil
}

func (u *Union) ErrorOnUnknown(typeName string) error {
return fmt.Errorf("invalid value in union type. Type name: %s", typeName)
}
Expand All @@ -115,12 +143,18 @@ func (u *Union) Accept(v UnionVisitor) error {
return v.VisitOne(*u.one)
case "two":
return v.VisitTwo(*u.two)
case "three":
return v.VisitThree(*u.three)
case "four":
return v.VisitFour(*u.four)
}
}

type UnionVisitor interface {
VisitOne(v api.Struct1) error
VisitTwo(v api1.Struct2) error
VisitThree(v v2.ObjectInPackageEndingInVersion) error
VisitFour(v v21.DifferentPackageEndingInVersion) error
VisitUnknown(typeName string) error
}

Expand All @@ -135,12 +169,18 @@ func (u *Union) AcceptWithContext(ctx context.Context, v UnionVisitorWithContext
return v.VisitOneWithContext(ctx, *u.one)
case "two":
return v.VisitTwoWithContext(ctx, *u.two)
case "three":
return v.VisitThreeWithContext(ctx, *u.three)
case "four":
return v.VisitFourWithContext(ctx, *u.four)
}
}

type UnionVisitorWithContext interface {
VisitOneWithContext(ctx context.Context, v api.Struct1) error
VisitTwoWithContext(ctx context.Context, v api1.Struct2) error
VisitThreeWithContext(ctx context.Context, v v2.ObjectInPackageEndingInVersion) error
VisitFourWithContext(ctx context.Context, v v21.DifferentPackageEndingInVersion) error
VisitUnknownWithContext(ctx context.Context, typeName string) error
}

Expand All @@ -151,3 +191,11 @@ func NewUnionFromOne(v api.Struct1) Union {
func NewUnionFromTwo(v api1.Struct2) Union {
return Union{typ: "two", two: &v}
}

func NewUnionFromThree(v v2.ObjectInPackageEndingInVersion) Union {
return Union{typ: "three", three: &v}
}

func NewUnionFromFour(v v21.DifferentPackageEndingInVersion) Union {
return Union{typ: "four", four: &v}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"github.com/palantir/conjure-go/v6/integration_test/testgenerated/imports/pkg1/api"
api1 "github.com/palantir/conjure-go/v6/integration_test/testgenerated/imports/pkg2/api"
v2 "github.com/palantir/conjure-go/v6/integration_test/testgenerated/imports/pkg4/v2"
v21 "github.com/palantir/conjure-go/v6/integration_test/testgenerated/imports/pkg5/v2"
)

type UnionWithT[T any] Union
Expand All @@ -26,11 +28,17 @@ func (u *UnionWithT[T]) Accept(ctx context.Context, v UnionVisitorWithT[T]) (T,
return v.VisitOne(ctx, *u.one)
case "two":
return v.VisitTwo(ctx, *u.two)
case "three":
return v.VisitThree(ctx, *u.three)
case "four":
return v.VisitFour(ctx, *u.four)
}
}

type UnionVisitorWithT[T any] interface {
VisitOne(ctx context.Context, v api.Struct1) (T, error)
VisitTwo(ctx context.Context, v api1.Struct2) (T, error)
VisitThree(ctx context.Context, v v2.ObjectInPackageEndingInVersion) (T, error)
VisitFour(ctx context.Context, v v21.DifferentPackageEndingInVersion) (T, error)
VisitUnknown(ctx context.Context, typ string) (T, error)
}
28 changes: 28 additions & 0 deletions integration_test/testgenerated/imports/pkg4/v2/structs.conjure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This file was generated by Conjure and should not be manually edited.

package v2

import (
"github.com/palantir/pkg/safejson"
"github.com/palantir/pkg/safeyaml"
)

type ObjectInPackageEndingInVersion struct {
Name string `json:"name"`
}

func (o ObjectInPackageEndingInVersion) MarshalYAML() (interface{}, error) {
jsonBytes, err := safejson.Marshal(o)
if err != nil {
return nil, err
}
return safeyaml.JSONtoYAMLMapSlice(jsonBytes)
}

func (o *ObjectInPackageEndingInVersion) UnmarshalYAML(unmarshal func(interface{}) error) error {
jsonBytes, err := safeyaml.UnmarshalerToJSONBytes(unmarshal)
if err != nil {
return err
}
return safejson.Unmarshal(jsonBytes, *&o)
}
28 changes: 28 additions & 0 deletions integration_test/testgenerated/imports/pkg5/v2/structs.conjure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This file was generated by Conjure and should not be manually edited.

package v2

import (
"github.com/palantir/pkg/safejson"
"github.com/palantir/pkg/safeyaml"
)

type DifferentPackageEndingInVersion struct {
Name string `json:"name"`
}

func (o DifferentPackageEndingInVersion) MarshalYAML() (interface{}, error) {
jsonBytes, err := safejson.Marshal(o)
if err != nil {
return nil, err
}
return safeyaml.JSONtoYAMLMapSlice(jsonBytes)
}

func (o *DifferentPackageEndingInVersion) UnmarshalYAML(unmarshal func(interface{}) error) error {
jsonBytes, err := safeyaml.UnmarshalerToJSONBytes(unmarshal)
if err != nil {
return err
}
return safejson.Unmarshal(jsonBytes, *&o)
}

0 comments on commit 201c9c7

Please sign in to comment.