Skip to content

Commit

Permalink
#minor Enum type (#183)
Browse files Browse the repository at this point in the history
* Enum type definition

Signed-off-by: Ketan Umare <[email protected]>

* Enum Type support in flyteidl

Signed-off-by: Ketan Umare <[email protected]>

* lint fixes

Signed-off-by: Ketan Umare <[email protected]>

* re-generated

Signed-off-by: Ketan Umare <[email protected]>

* Enum default values are the first value in the list

Signed-off-by: Ketan Umare <[email protected]>
  • Loading branch information
kumare3 authored Jun 7, 2021
1 parent e324c12 commit 2909e39
Show file tree
Hide file tree
Showing 31 changed files with 2,880 additions and 8,740 deletions.
32 changes: 32 additions & 0 deletions flyteidl/clients/go/coreutils/literals.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ func MakeDefaultLiteralForType(typ *core.LiteralType) (*core.Literal, error) {
},
},
}, nil
case *core.LiteralType_EnumType:
return MakeLiteralForType(typ, nil)
//case *core.LiteralType_Schema:
}

Expand Down Expand Up @@ -462,6 +464,7 @@ func MakeLiteralForType(t *core.LiteralType, v interface{}) (*core.Literal, erro
Literals: literals,
},
}

case *core.LiteralType_CollectionType:
newT := t.Type.(*core.LiteralType_CollectionType)
newV, ok := v.([]interface{})
Expand All @@ -482,6 +485,7 @@ func MakeLiteralForType(t *core.LiteralType, v interface{}) (*core.Literal, erro
Literals: literals,
},
}

case *core.LiteralType_Simple:
newT := t.Type.(*core.LiteralType_Simple)
strValue := fmt.Sprintf("%v", v)
Expand All @@ -499,11 +503,39 @@ func MakeLiteralForType(t *core.LiteralType, v interface{}) (*core.Literal, erro
return nil, err
}
return lv, nil

case *core.LiteralType_Blob:
newT := t.Type.(*core.LiteralType_Blob)
isDir := newT.Blob.Dimensionality == core.BlobType_MULTIPART
lv := MakeLiteralForBlob(storage.DataReference(fmt.Sprintf("%v", v)), isDir, newT.Blob.Format)
return lv, nil

case *core.LiteralType_EnumType:
var newV string
if v == nil {
if len(t.GetEnumType().Values) == 0 {
return nil, fmt.Errorf("enum types need atleast one value")
}
newV = t.GetEnumType().Values[0]
} else {
var ok bool
newV, ok = v.(string)
if !ok {
return nil, fmt.Errorf("cannot convert [%v] to enum representations, only string values are supported in enum literals", reflect.TypeOf(v))
}
found := false
for _, val := range t.GetEnumType().GetValues() {
if val == newV {
found = true
break
}
}
if !found {
return nil, fmt.Errorf("incorrect enum value [%s], supported values %+v", newV, t.GetEnumType().GetValues())
}
}
return MakePrimitiveLiteral(newV)

default:
return nil, fmt.Errorf("unsupported type %s", t.String())
}
Expand Down
36 changes: 36 additions & 0 deletions flyteidl/clients/go/coreutils/literals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,17 @@ func TestMakeDefaultLiteralForType(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, l.GetScalar().GetGeneric())
})

t.Run("enum", func(t *testing.T) {
l, err := MakeDefaultLiteralForType(&core.LiteralType{Type: &core.LiteralType_EnumType{
EnumType: &core.EnumType{Values: []string{"x", "y", "z"}},
}})
assert.NoError(t, err)
assert.NotNil(t, l.GetScalar().GetPrimitive().GetStringValue())
expected := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{Primitive: &core.Primitive{Value: &core.Primitive_StringValue{StringValue: "x"}}}}}}
assert.Equal(t, expected, l)
})
}

func TestMustMakeDefaultLiteralForType(t *testing.T) {
Expand Down Expand Up @@ -581,4 +592,29 @@ func TestMakeLiteralForType(t *testing.T) {
assert.Equal(t, expectedErrorf, err)

})

t.Run("enumtype-nil", func(t *testing.T) {
var literalType = &core.LiteralType{Type: &core.LiteralType_EnumType{EnumType: &core.EnumType{}}}
_, err := MakeLiteralForType(literalType, nil)
assert.Error(t, err)
_, err = MakeLiteralForType(literalType, "")
assert.Error(t, err)
})

t.Run("enumtype-happy", func(t *testing.T) {
var literalType = &core.LiteralType{Type: &core.LiteralType_EnumType{EnumType: &core.EnumType{Values: []string{"x", "y", "z"}}}}
expected := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{Primitive: &core.Primitive{Value: &core.Primitive_StringValue{StringValue: "x"}}}}}}
v, err := MakeLiteralForType(literalType, "x")
assert.NoError(t, err)
assert.Equal(t, expected, v)
_, err = MakeLiteralForType(literalType, "")
assert.Error(t, err)
})

t.Run("enumtype-illegal-val", func(t *testing.T) {
var literalType = &core.LiteralType{Type: &core.LiteralType_EnumType{EnumType: &core.EnumType{Values: []string{"x", "y", "z"}}}}
_, err := MakeLiteralForType(literalType, "m")
assert.Error(t, err)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/flyteorg/flyteidl/clients/go/admin"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/event"
"github.com/golang/protobuf/ptypes"
"github.com/flyteorg/flytestdlib/config"
"github.com/golang/protobuf/ptypes"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion flyteidl/gen/pb-cpp/flyteidl/core/interface.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2909e39

Please sign in to comment.