From 4f98b2e604bf6e97bef8dce739b2844dec5608d6 Mon Sep 17 00:00:00 2001 From: Sam Lai Date: Mon, 29 Mar 2021 14:46:03 +0100 Subject: [PATCH] [go] Add blob support for MakeLiteralForType The MakeLiteralForType client method currently doesn't support the blob type. This adds support for that. Signed-off-by: Sam Lai --- clients/go/coreutils/extract_literal.go | 5 +++ clients/go/coreutils/literals.go | 5 +++ clients/go/coreutils/literals_test.go | 56 +++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/clients/go/coreutils/extract_literal.go b/clients/go/coreutils/extract_literal.go index 0d7d0454b..4c4ae1f5a 100644 --- a/clients/go/coreutils/extract_literal.go +++ b/clients/go/coreutils/extract_literal.go @@ -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: diff --git a/clients/go/coreutils/literals.go b/clients/go/coreutils/literals.go index a331ee3b0..6bb30f7be 100644 --- a/clients/go/coreutils/literals.go +++ b/clients/go/coreutils/literals.go @@ -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()) } diff --git a/clients/go/coreutils/literals_test.go b/clients/go/coreutils/literals_test.go index 9bcb39a09..1106e2bcc 100644 --- a/clients/go/coreutils/literals_test.go +++ b/clients/go/coreutils/literals_test.go @@ -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{}}}