Skip to content

Commit

Permalink
Revert "Ensure that imports are not removed in generated code (#406)"
Browse files Browse the repository at this point in the history
This reverts commit ffd6daa.
  • Loading branch information
nmiyake committed Jan 30, 2023
1 parent 1282333 commit a5508f7
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 89 deletions.
16 changes: 0 additions & 16 deletions conjure/conjure.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
package conjure

import (
"os"
"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/palantir/go-ptimports/v2/ptimports"
"github.com/pkg/errors"
)

Expand All @@ -32,25 +30,11 @@ func Generate(conjureDefinition spec.ConjureDefinition, outputConfiguration Outp
if err != nil {
return err
}
// write the generated files
for _, file := range files {
if err := file.Write(); err != nil {
return err
}
}
// format all the generated files after they have been written.
// Must be done after all the files are written to ensure that imports are processed correctly (goimports adds named
// aliases for imports where the inferred package name differs from the actual package name, and this can only be
// properly determined after all generated code is written -- see https://github.com/palantir/conjure-go/issues/405).
for _, file := range files {
goFileSrc, err := ptimports.Process(file.absPath, nil, nil)
if err != nil {
return errors.Wrapf(err, "failed to run ptimports on generated Go source for file %s", file.absPath)
}
if err := os.WriteFile(file.absPath, goFileSrc, 0644); err != nil {
return errors.Wrapf(err, "failed to write file")
}
}
return nil
}

Expand Down
5 changes: 1 addition & 4 deletions conjure/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ func (f *OutputFile) Render() ([]byte, error) {
return nil, errors.Wrapf(err, "failed to generate Go source for file %s", f.absPath)
}

goFileSrc, err := ptimports.Process("", buf.Bytes(), &ptimports.Options{
Refactor: true,
FormatOnly: true,
})
goFileSrc, err := ptimports.Process("", buf.Bytes(), &ptimports.Options{Refactor: true})
if err != nil {
return nil, errors.Wrapf(err, "failed to run ptimports on generated Go source for file %s", f.absPath)
}
Expand Down
5 changes: 0 additions & 5 deletions integration_test/testgenerated/imports/imports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@ types:
package: com.palantir.pkg2.api
fields:
data: Struct1
ObjectInPackageEndingInVersion:
package: com.palantir.pkg4.v2
fields:
name: string
Union:
package: com.palantir.pkg3.api
union:
one: Struct1
two: Struct2
three: ObjectInPackageEndingInVersion
13 changes: 13 additions & 0 deletions integration_test/testgenerated/imports/imports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package imports

import (
"testing"

"github.com/palantir/conjure-go/v6/integration_test/testgenerated/imports/pkg3/api"
)

// TestImportNameConflict ensures that we can import a type which includes types of the same package name.
// This is only testing that the relevant code compiles, and does not do anything in the test body.
func TestImportNameConflict(t *testing.T) {
_ = api.Union{}
}
40 changes: 8 additions & 32 deletions integration_test/testgenerated/imports/pkg3/api/unions.conjure.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,24 @@ 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"
"github.com/palantir/pkg/safejson"
"github.com/palantir/pkg/safeyaml"
)

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

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

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

func (u *Union) toSerializer() (interface{}, error) {
Expand All @@ -45,11 +42,6 @@ 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
}
}

Expand Down Expand Up @@ -86,7 +78,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, threeFunc func(v2.ObjectInPackageEndingInVersion) error, unknownFunc func(string) error) error {
func (u *Union) AcceptFuncs(oneFunc func(api.Struct1) error, twoFunc func(api1.Struct2) error, unknownFunc func(string) error) error {
switch u.typ {
default:
if u.typ == "" {
Expand All @@ -97,8 +89,6 @@ 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)
}
}

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

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

func (u *Union) ErrorOnUnknown(typeName string) error {
return fmt.Errorf("invalid value in union type. Type name: %s", typeName)
}
Expand All @@ -129,15 +115,12 @@ 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)
}
}

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

Expand All @@ -152,15 +135,12 @@ 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)
}
}

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
VisitUnknownWithContext(ctx context.Context, typeName string) error
}

Expand All @@ -171,7 +151,3 @@ 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}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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"
)

type UnionWithT[T any] Union
Expand All @@ -27,14 +26,11 @@ 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)
}
}

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)
VisitUnknown(ctx context.Context, typ string) (T, error)
}
28 changes: 0 additions & 28 deletions integration_test/testgenerated/imports/pkg4/v2/structs.conjure.go

This file was deleted.

0 comments on commit a5508f7

Please sign in to comment.