Skip to content

Commit

Permalink
wiring factories and default values missed in codegen
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Mortari <[email protected]>
  • Loading branch information
tarilabs committed Feb 22, 2024
1 parent ce656aa commit 2edb456
Show file tree
Hide file tree
Showing 13 changed files with 548 additions and 518 deletions.
55 changes: 13 additions & 42 deletions internal/converter/generated/openapi_converter.gen.go

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

16 changes: 8 additions & 8 deletions internal/converter/mlmd_converter_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestMetadataValueBool(t *testing.T) {
data := make(map[string]openapi.MetadataValue)
key := "my bool"
mdValue := true
data[key] = openapi.MetadataBoolValueAsMetadataValue(&openapi.MetadataBoolValue{BoolValue: &mdValue})
data[key] = openapi.MetadataBoolValueAsMetadataValue(&openapi.MetadataBoolValue{BoolValue: mdValue})

roundTripAndAssert(t, data, key)
}
Expand All @@ -75,7 +75,7 @@ func TestMetadataValueInt(t *testing.T) {
data := make(map[string]openapi.MetadataValue)
key := "my int"
mdValue := "987"
data[key] = openapi.MetadataIntValueAsMetadataValue(&openapi.MetadataIntValue{IntValue: &mdValue})
data[key] = openapi.MetadataIntValueAsMetadataValue(&openapi.MetadataIntValue{IntValue: mdValue})

roundTripAndAssert(t, data, key)
}
Expand All @@ -84,7 +84,7 @@ func TestMetadataValueIntFailure(t *testing.T) {
data := make(map[string]openapi.MetadataValue)
key := "my int"
mdValue := "not a number"
data[key] = openapi.MetadataIntValueAsMetadataValue(&openapi.MetadataIntValue{IntValue: &mdValue})
data[key] = openapi.MetadataIntValueAsMetadataValue(&openapi.MetadataIntValue{IntValue: mdValue})

assertion := setup(t)
asGRPC, err := MapOpenAPICustomProperties(&data)
Expand All @@ -97,7 +97,7 @@ func TestMetadataValueDouble(t *testing.T) {
data := make(map[string]openapi.MetadataValue)
key := "my double"
mdValue := 3.1415
data[key] = openapi.MetadataDoubleValueAsMetadataValue(&openapi.MetadataDoubleValue{DoubleValue: &mdValue})
data[key] = openapi.MetadataDoubleValueAsMetadataValue(&openapi.MetadataDoubleValue{DoubleValue: mdValue})

roundTripAndAssert(t, data, key)
}
Expand All @@ -106,7 +106,7 @@ func TestMetadataValueString(t *testing.T) {
data := make(map[string]openapi.MetadataValue)
key := "my string"
mdValue := "Hello, World!"
data[key] = openapi.MetadataStringValueAsMetadataValue(&openapi.MetadataStringValue{StringValue: &mdValue})
data[key] = openapi.MetadataStringValueAsMetadataValue(&openapi.MetadataStringValue{StringValue: mdValue})

roundTripAndAssert(t, data, key)
}
Expand All @@ -123,7 +123,7 @@ func TestMetadataValueStruct(t *testing.T) {
t.Error(err)
}
b64 := base64.StdEncoding.EncodeToString(asJSON)
data[key] = openapi.MetadataStructValueAsMetadataValue(&openapi.MetadataStructValue{StructValue: &b64})
data[key] = openapi.MetadataStructValueAsMetadataValue(&openapi.MetadataStructValue{StructValue: b64})

roundTripAndAssert(t, data, key)
}
Expand All @@ -142,8 +142,8 @@ func TestMetadataValueProtoUnsupported(t *testing.T) {
b64 := base64.StdEncoding.EncodeToString(asJSON)
typeDef := "map[string]openapi.MetadataValue"
data[key] = openapi.MetadataProtoValueAsMetadataValue(&openapi.MetadataProtoValue{
Type: &typeDef,
ProtoValue: &b64,
Type: typeDef,
ProtoValue: b64,
})

assertion := setup(t)
Expand Down
41 changes: 26 additions & 15 deletions internal/converter/mlmd_openapi_converter_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,34 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/kubeflow/model-registry/internal/constants"
"github.com/kubeflow/model-registry/internal/ml_metadata/proto"
"github.com/kubeflow/model-registry/pkg/openapi"
)

func NewMetadataStringValue(value string) *openapi.MetadataStringValue {
return openapi.NewMetadataStringValue(value, "MetadataStringValue")
}

func NewMetadataBoolValue(value bool) *openapi.MetadataBoolValue {
return openapi.NewMetadataBoolValue(value, "MetadataBoolValue")
}

func NewMetadataDoubleValue(value float64) *openapi.MetadataDoubleValue {
return openapi.NewMetadataDoubleValue(value, "MetadataDoubleValue")
}

func NewMetadataIntValue(value string) *openapi.MetadataIntValue {
return openapi.NewMetadataIntValue(value, "MetadataIntValue")
}

func NewMetadataStructValue(value string) *openapi.MetadataStructValue {
return openapi.NewMetadataStructValue(value, "MetadataStructValue")
}

// MapMLMDCustomProperties maps MLMD custom properties model to OpenAPI one
func MapMLMDCustomProperties(source map[string]*proto.Value) (map[string]openapi.MetadataValue, error) {
data := make(map[string]openapi.MetadataValue)
Expand All @@ -21,21 +42,13 @@ func MapMLMDCustomProperties(source map[string]*proto.Value) (map[string]openapi

switch typedValue := v.Value.(type) {
case *proto.Value_BoolValue:
customValue.MetadataBoolValue = &openapi.MetadataBoolValue{
BoolValue: &typedValue.BoolValue,
}
customValue.MetadataBoolValue = NewMetadataBoolValue(typedValue.BoolValue)
case *proto.Value_IntValue:
customValue.MetadataIntValue = &openapi.MetadataIntValue{
IntValue: Int64ToString(&typedValue.IntValue),
}
customValue.MetadataIntValue = NewMetadataIntValue(strconv.FormatInt(typedValue.IntValue, 10))
case *proto.Value_DoubleValue:
customValue.MetadataDoubleValue = &openapi.MetadataDoubleValue{
DoubleValue: &typedValue.DoubleValue,
}
customValue.MetadataDoubleValue = NewMetadataDoubleValue(typedValue.DoubleValue)
case *proto.Value_StringValue:
customValue.MetadataStringValue = &openapi.MetadataStringValue{
StringValue: &typedValue.StringValue,
}
customValue.MetadataStringValue = NewMetadataStringValue(typedValue.StringValue)
case *proto.Value_StructValue:
sv := typedValue.StructValue
asMap := sv.AsMap()
Expand All @@ -44,9 +57,7 @@ func MapMLMDCustomProperties(source map[string]*proto.Value) (map[string]openapi
return nil, err
}
b64 := base64.StdEncoding.EncodeToString(asJSON)
customValue.MetadataStructValue = &openapi.MetadataStructValue{
StructValue: &b64,
}
customValue.MetadataStructValue = NewMetadataStructValue(b64)
default:
return nil, fmt.Errorf("type mapping not found for %s:%v", key, v)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/converter/openapi_mlmd_converter_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ func MapOpenAPICustomProperties(source *map[string]openapi.MetadataValue) (map[s
switch {
// bool value
case v.MetadataBoolValue != nil:
value.Value = &proto.Value_BoolValue{BoolValue: *v.MetadataBoolValue.BoolValue}
value.Value = &proto.Value_BoolValue{BoolValue: v.MetadataBoolValue.BoolValue}
// int value
case v.MetadataIntValue != nil:
intValue, err := StringToInt64(v.MetadataIntValue.IntValue)
intValue, err := StringToInt64(&v.MetadataIntValue.IntValue)
if err != nil {
return nil, fmt.Errorf("unable to decode as int64 %w for key %s", err, key)
}
value.Value = &proto.Value_IntValue{IntValue: *intValue}
// double value
case v.MetadataDoubleValue != nil:
value.Value = &proto.Value_DoubleValue{DoubleValue: *v.MetadataDoubleValue.DoubleValue}
value.Value = &proto.Value_DoubleValue{DoubleValue: v.MetadataDoubleValue.DoubleValue}
// string value
case v.MetadataStringValue != nil:
value.Value = &proto.Value_StringValue{StringValue: *v.MetadataStringValue.StringValue}
value.Value = &proto.Value_StringValue{StringValue: v.MetadataStringValue.StringValue}
// struct value
case v.MetadataStructValue != nil:
data, err := base64.StdEncoding.DecodeString(*v.MetadataStructValue.StructValue)
data, err := base64.StdEncoding.DecodeString(v.MetadataStructValue.StructValue)
if err != nil {
return nil, fmt.Errorf("unable to decode %w for key %s", err, key)
}
Expand Down
Loading

0 comments on commit 2edb456

Please sign in to comment.