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

[go] Add blob support for MakeLiteralForType #131

Merged
merged 1 commit into from
Mar 30, 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
5 changes: 5 additions & 0 deletions clients/go/coreutils/extract_literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ func ExtractFromLiteral(literal *core.Literal) (interface{}, error) {
case *core.Primitive_Duration:
scalarPrimitiveDuration := scalarPrimitive.Duration
return scalarPrimitiveDuration, nil
default:
return nil, fmt.Errorf("unsupported literal scalar primitive type %T", scalarValue)
}
case *core.Scalar_Blob:
return scalarValue.Blob.Uri, nil
default:
return nil, fmt.Errorf("unsupported literal scalar type %T", scalarValue)
}
case *core.Literal_Collection:
Expand Down
5 changes: 5 additions & 0 deletions clients/go/coreutils/literals.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,11 @@ 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
default:
return nil, fmt.Errorf("unsupported type %s", t.String())
}
Expand Down
56 changes: 56 additions & 0 deletions clients/go/coreutils/literals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,62 @@ func TestMakeLiteralForType(t *testing.T) {
assert.Equal(t, expectedVal, actualVal)
})

t.Run("Blob", func(t *testing.T) {
var literalType = &core.LiteralType{Type: &core.LiteralType_Blob{Blob: &core.BlobType{
Dimensionality: core.BlobType_SINGLE,
}}}
expectedLV := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Value: &core.Scalar_Blob{
Blob: &core.Blob{
Uri: "s3://blah/blah/blah",
Metadata: &core.BlobMetadata{
Type: &core.BlobType{
Dimensionality: core.BlobType_SINGLE,
},
},
},
},
}}}
lv, err := MakeLiteralForType(literalType, "s3://blah/blah/blah")
assert.NoError(t, err)

assert.Equal(t, expectedLV, lv)

expectedVal, err := ExtractFromLiteral(expectedLV)
assert.NoError(t, err)
actualVal, err := ExtractFromLiteral(lv)
assert.NoError(t, err)
assert.Equal(t, expectedVal, actualVal)
})

t.Run("MultipartBlob", func(t *testing.T) {
var literalType = &core.LiteralType{Type: &core.LiteralType_Blob{Blob: &core.BlobType{
Dimensionality: core.BlobType_MULTIPART,
}}}
expectedLV := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Value: &core.Scalar_Blob{
Blob: &core.Blob{
Uri: "s3://blah/blah/blah",
Metadata: &core.BlobMetadata{
Type: &core.BlobType{
Dimensionality: core.BlobType_MULTIPART,
},
},
},
},
}}}
lv, err := MakeLiteralForType(literalType, "s3://blah/blah/blah")
assert.NoError(t, err)

assert.Equal(t, expectedLV, lv)

expectedVal, err := ExtractFromLiteral(expectedLV)
assert.NoError(t, err)
actualVal, err := ExtractFromLiteral(lv)
assert.NoError(t, err)
assert.Equal(t, expectedVal, actualVal)
})

t.Run("UnsupportedType", func(t *testing.T) {
var literalType = &core.LiteralType{Type: &core.LiteralType_Schema{
Schema: &core.SchemaType{}}}
Expand Down