Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
Allow string to enum casting
Browse files Browse the repository at this point in the history
Signed-off-by: Ketan Umare <[email protected]>
  • Loading branch information
kumare3 committed Jun 11, 2021
1 parent 4d3068f commit 5a03a3e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pkg/compiler/validators/typing.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func (t trivialChecker) CastsFrom(upstreamType *flyte.LiteralType) bool {
return true
}
}
// If t is an enum, it can be created from a string as Enums as just constrained String aliases
if t.literalType.GetEnumType() != nil {
if upstreamType.GetSimple() == flyte.SimpleType_STRING {
return true
}
}

// Ignore metadata when comparing types.
upstreamTypeCopy := *upstreamType
Expand Down
22 changes: 19 additions & 3 deletions pkg/compiler/validators/typing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestSimpleLiteralCasting(t *testing.T) {
Type: &core.LiteralType_Simple{Simple: core.SimpleType_STRING},
},
)
assert.True(t, castable, "Integers should be castable to other integers")
assert.True(t, castable, "Enum should be castable to string")
})

t.Run("EnumToEnum", func(t *testing.T) {
Expand All @@ -102,7 +102,23 @@ func TestSimpleLiteralCasting(t *testing.T) {
}},
},
)
assert.True(t, castable, "Integers should be castable to other integers")
assert.True(t, castable, "Enum should be castable to Enums if they are identical")
})

t.Run("EnumToEnum", func(t *testing.T) {
castable := AreTypesCastable(
&core.LiteralType{
Type: &core.LiteralType_EnumType{EnumType: &core.EnumType{
Values: []string{"x", "y"},
}},
},
&core.LiteralType{
Type: &core.LiteralType_EnumType{EnumType: &core.EnumType{
Values: []string{"m", "n"},
}},
},
)
assert.False(t, castable, "Enum should not be castable to non matching enums")
})

t.Run("StringToEnum", func(t *testing.T) {
Expand All @@ -116,7 +132,7 @@ func TestSimpleLiteralCasting(t *testing.T) {
}},
},
)
assert.False(t, castable, "Integers should be castable to other integers")
assert.True(t, castable, "Strings should be castable to enums - may result in runtime failure")
})
}

Expand Down

0 comments on commit 5a03a3e

Please sign in to comment.