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

Extract OverlayWith() method #1598

Merged
merged 2 commits into from
Jun 24, 2021
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
8 changes: 8 additions & 0 deletions hack/generator/pkg/astmodel/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ func (types Types) Contains(name TypeName) bool {
return ok
}

// OverlayWith creates a new set containing all the type definitions from both this and the provided set. Any name
// collisions are resolved in favour of the provided set. Returns a new independent set, leaving the original unmodified.
func (types Types) OverlayWith(t Types) Types {
result := t.Copy()
result.AddTypes(types.Except(t))
return result
}

// TypesDisjointUnion merges this and other, with a safety check that no type is overwritten.
// If an attempt is made to overwrite a type, this function panics
func TypesDisjointUnion(s1 Types, s2 Types) Types {
Expand Down
47 changes: 40 additions & 7 deletions hack/generator/pkg/astmodel/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
)

var (
pkg = MakeExternalPackageReference("foo")
alphaDefinition = createTestDefinition("alpha")
betaDefinition = createTestDefinition("beta")
gammaDefinition = createTestDefinition("gamma")
deltaDefinition = createTestDefinition("delta")
pkg = MakeExternalPackageReference("foo")
alphaDefinition = createTestDefinition("alpha", StringType)
betaDefinition = createTestDefinition("beta", StringType)
gammaDefinition = createTestDefinition("gamma", StringType)
deltaDefinition = createTestDefinition("delta", StringType)
deltaIntDefinition = createTestDefinition("delta", IntType)
)

/*
Expand Down Expand Up @@ -146,13 +147,45 @@ func Test_TypesExcept_GivenSubset_ReturnsExpectedSet(t *testing.T) {
g.Expect(set).To(ContainElement(deltaDefinition))
}

/*
* Overlay() tests
*/

func Test_TypesOverlayWith_GivenDisjointSets_ReturnsUnionSet(t *testing.T) {
g := NewGomegaWithT(t)
left := createTestTypes(alphaDefinition, betaDefinition)
right := createTestTypes(gammaDefinition, deltaDefinition)

set := left.OverlayWith(right)

g.Expect(len(set)).To(Equal(4))
g.Expect(set).To(ContainElement(alphaDefinition))
g.Expect(set).To(ContainElement(betaDefinition))
g.Expect(set).To(ContainElement(gammaDefinition))
g.Expect(set).To(ContainElement(deltaDefinition))
}

func Test_TypesOverlayWith_GivenOverlappingSets_PrefersTypeInOverlay(t *testing.T) {
g := NewGomegaWithT(t)
left := createTestTypes(alphaDefinition, deltaDefinition)
right := createTestTypes(gammaDefinition, deltaIntDefinition)

set := left.OverlayWith(right)

g.Expect(len(set)).To(Equal(3))
g.Expect(set).To(ContainElement(alphaDefinition))
g.Expect(set).To(ContainElement(gammaDefinition))
g.Expect(set).To(ContainElement(deltaIntDefinition))
g.Expect(set).NotTo(ContainElement(deltaDefinition))
}

/*
* Utility functions
*/

func createTestDefinition(name string) TypeDefinition {
func createTestDefinition(name string, underlyingType Type) TypeDefinition {
n := MakeTypeName(pkg, name)
return MakeTypeDefinition(n, StringType)
return MakeTypeDefinition(n, underlyingType)
}

func createTestTypes(defs ...TypeDefinition) Types {
Expand Down