Skip to content

Commit

Permalink
Improve literal type string representation handling (#5932)
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Su <[email protected]>
  • Loading branch information
pingsutw authored Nov 1, 2024
1 parent 54a4ad9 commit a2988ba
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
23 changes: 23 additions & 0 deletions flytepropeller/pkg/compiler/common/pretty_print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package common

import (
"fmt"
"strings"

"github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/core"
)

func LiteralTypeToStr(lt *core.LiteralType) string {
if lt == nil {
return "None"
}
if lt.GetSimple() == core.SimpleType_STRUCT {
var structure string
for k, v := range lt.GetStructure().GetDataclassType() {
structure += fmt.Sprintf("dataclass_type:{key:%v value:{%v}, ", k, LiteralTypeToStr(v))
}
structure = strings.TrimSuffix(structure, ", ")
return fmt.Sprintf("simple: STRUCT structure{%v}", structure)
}
return lt.String()
}
36 changes: 36 additions & 0 deletions flytepropeller/pkg/compiler/common/pretty_print_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package common

import (
"testing"

"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/structpb"

"github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/core"
)

func TestLiteralTypeToStr(t *testing.T) {
dataclassType := &core.LiteralType{
Type: &core.LiteralType_Simple{Simple: core.SimpleType_STRUCT},
Structure: &core.TypeStructure{
DataclassType: map[string]*core.LiteralType{
"a": {
Type: &core.LiteralType_Simple{Simple: core.SimpleType_INTEGER},
},
},
},
Metadata: &structpb.Struct{Fields: map[string]*structpb.Value{
"key": {Kind: &structpb.Value_StringValue{StringValue: "a"}},
}},
}
assert.Equal(t, LiteralTypeToStr(nil), "None")
assert.Equal(t, LiteralTypeToStr(dataclassType), "simple: STRUCT structure{dataclass_type:{key:a value:{simple:INTEGER}}")
assert.NotEqual(t, LiteralTypeToStr(dataclassType), dataclassType.String())

// Test for SimpleType
simpleType := &core.LiteralType{
Type: &core.LiteralType_Simple{Simple: core.SimpleType_INTEGER},
}
assert.Equal(t, LiteralTypeToStr(simpleType), "simple:INTEGER")
assert.Equal(t, LiteralTypeToStr(simpleType), simpleType.String())
}
2 changes: 1 addition & 1 deletion flytepropeller/pkg/compiler/transformers/k8s/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func validateInputs(nodeID common.NodeID, iface *core.TypedInterface, inputs cor
continue
}
if !validators.AreTypesCastable(inputType, v.Type) {
errs.Collect(errors.NewMismatchingTypesErr(nodeID, inputVar, v.Type.String(), inputType.String()))
errs.Collect(errors.NewMismatchingTypesErr(nodeID, inputVar, common.LiteralTypeToStr(v.Type), common.LiteralTypeToStr(inputType)))
continue
}

Expand Down
6 changes: 3 additions & 3 deletions flytepropeller/pkg/compiler/validators/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func validateBinding(w c.WorkflowBuilder, node c.Node, nodeParam string, binding
// If the variable has an index. We expect param to be a collection.
if v.Index != nil {
if cType := param.GetType().GetCollectionType(); cType == nil {
errs.Collect(errors.NewMismatchingVariablesErr(nodeID, outputVar, param.Type.String(), inputVar, expectedType.String()))
errs.Collect(errors.NewMismatchingVariablesErr(nodeID, outputVar, c.LiteralTypeToStr(param.Type), inputVar, c.LiteralTypeToStr(expectedType)))
} else {
sourceType = cType
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func validateBinding(w c.WorkflowBuilder, node c.Node, nodeParam string, binding
return param.GetType(), []c.NodeID{val.Promise.NodeId}, true
}

errs.Collect(errors.NewMismatchingVariablesErr(node.GetId(), outputVar, sourceType.String(), inputVar, expectedType.String()))
errs.Collect(errors.NewMismatchingVariablesErr(node.GetId(), outputVar, c.LiteralTypeToStr(sourceType), inputVar, c.LiteralTypeToStr(expectedType)))
return nil, nil, !errs.HasErrors()
}
}
Expand All @@ -180,7 +180,7 @@ func validateBinding(w c.WorkflowBuilder, node c.Node, nodeParam string, binding
if literalType == nil {
errs.Collect(errors.NewUnrecognizedValueErr(nodeID, reflect.TypeOf(val.Scalar.GetValue()).String()))
} else if validateParamTypes && !AreTypesCastable(literalType, expectedType) {
errs.Collect(errors.NewMismatchingTypesErr(nodeID, nodeParam, literalType.String(), expectedType.String()))
errs.Collect(errors.NewMismatchingTypesErr(nodeID, nodeParam, c.LiteralTypeToStr(literalType), c.LiteralTypeToStr(expectedType)))
}

if expectedType.GetEnumType() != nil {
Expand Down
2 changes: 1 addition & 1 deletion flytepropeller/pkg/compiler/validators/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func ValidateBooleanExpression(w c.WorkflowBuilder, node c.NodeBuilder, expr *fl
if op1Valid && op2Valid && op1Type != nil && op2Type != nil {
if op1Type.String() != op2Type.String() {
errs.Collect(errors.NewMismatchingTypesErr(node.GetId(), "RightValue",
op1Type.String(), op2Type.String()))
c.LiteralTypeToStr(op1Type), c.LiteralTypeToStr(op2Type)))
}
}
} else if expr.GetConjunction() != nil {
Expand Down
2 changes: 1 addition & 1 deletion flytepropeller/pkg/compiler/validators/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func validateInputVar(n c.NodeBuilder, paramName string, requireParamType bool,
func validateVarType(nodeID c.NodeID, paramName string, param *flyte.Variable,
expectedType *flyte.LiteralType, errs errors.CompileErrors) (ok bool) {
if param.GetType().String() != expectedType.String() {
errs.Collect(errors.NewMismatchingTypesErr(nodeID, paramName, param.GetType().String(), expectedType.String()))
errs.Collect(errors.NewMismatchingTypesErr(nodeID, paramName, c.LiteralTypeToStr(param.GetType()), c.LiteralTypeToStr(expectedType)))
}

return !errs.HasErrors()
Expand Down

0 comments on commit a2988ba

Please sign in to comment.