Defaults to application/json for cosmosdb in init()

This commit is contained in:
Artur Souza 2021-03-16 15:10:40 -07:00
parent 0d44da776c
commit 4f8e7f3358
2 changed files with 13 additions and 5 deletions

View File

@ -8,6 +8,7 @@ package cosmosdb
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strings"
@ -82,12 +83,19 @@ func (c *StateStore) Init(meta state.Metadata) error {
return err
}
var m metadata
m := metadata{
ContentType: "application/json",
}
err = json.Unmarshal(b, &m)
if err != nil {
return err
}
if m.ContentType == "" {
return errors.New("contentType is required")
}
client := documentdb.New(m.URL, &documentdb.Config{
MasterKey: &documentdb.Key{
Key: m.MasterKey,
@ -342,7 +350,7 @@ func (c *StateStore) Multi(request *state.TransactionalStateRequest) error {
func createUpsertItem(contentType string, req state.SetRequest, partitionKey string) CosmosItem {
byteArray, isBinary := req.Value.([]uint8)
if isBinary {
if contentType == "" || contenttype.IsJSONContentType(contentType) {
if contenttype.IsJSONContentType(contentType) {
var value map[string]interface{}
err := json.Unmarshal(byteArray, &value)
// if byte array is not a valid JSON, so keep it as-is to be Base64 encoded in CosmosDB.

View File

@ -26,7 +26,7 @@ func TestCreateCosmosItem(t *testing.T) {
Value: value,
}
item := createUpsertItem("", req, partitionKey)
item := createUpsertItem("application/json", req, partitionKey)
assert.Equal(t, partitionKey, item.PartitionKey)
assert.Equal(t, "testKey", item.ID)
assert.Equal(t, value, item.Value)
@ -55,7 +55,7 @@ func TestCreateCosmosItem(t *testing.T) {
Value: bytes,
}
item := createUpsertItem("", req, partitionKey)
item := createUpsertItem("application/json", req, partitionKey)
assert.Equal(t, partitionKey, item.PartitionKey)
assert.Equal(t, "testKey", item.ID)
@ -111,7 +111,7 @@ func TestCreateCosmosItem(t *testing.T) {
Value: bytes,
}
item := createUpsertItem("", req, partitionKey)
item := createUpsertItem("application/json", req, partitionKey)
assert.Equal(t, partitionKey, item.PartitionKey)
assert.Equal(t, "testKey", item.ID)