kidCoder
kidCoder

Reputation: 354

GCP Data Store: cannot store different Value Types in single Array-type property index

I am just getting started with GCP, and the documentation is a bit confusing (there is quite a bit, but there are also many ways to do a single thing).

I have an Entity with a property of type Array. I can add multiple stringValue-type kv pairs to a single index in the array, like so:

    "values": [
    {   
      "stringValue": "google.com",
      "stringValue": "k"
    },
    {
      "stringValue": "facebook.com"
    }
  ]

without GCP taking issue, but if I try to change this to

   "values": [
    {   
      "stringValue": "google.com",
      "timestampValue": "xxxxxxxxxx"
    },
    {
      "stringValue": "facebook.com"
    }
  ]

it won't let me save. Do I absolutely have to use a nested entity for this?

Upvotes: 1

Views: 424

Answers (1)

Sai Pullabhotla
Sai Pullabhotla

Reputation: 2207

Elements in an Array type can be of any supported types, except another Array. This means, an Array type can have elements that are Strings, Integer, Float, Timestamp, Null, EmbeddedEntities etc.

A properly formatted Array in GCD Console looks like the one below. This Array has two String elements, one Integer and one timestamp.

{
  "values": [
    {
      "stringValue": "propertyValue1"
    },
    {
      "stringValue": "propertyValue2"
    },
    {
      "integerValue": "300"
    },
    {
      "timestampValue": "2014-10-02T15:01:23.045123Z"
    }
  ]
}

If you need to store embedded entities in an Array, it would look like this:

{
  "values": [
    {
      "entityValue": {
        "properties": {
          "countryCode": {
            "stringValue": "91"
          },
          "subscriberNumber": {
            "stringValue": "2722 5858"
          },
          "areaCode": {
            "stringValue": "40"
          }
        }
      }
    },
    {
      "entityValue": {
        "properties": {
          "subscriberNumber": {
            "stringValue": "6666 0000"
          },
          "areaCode": {
            "stringValue": "80"
          },
          "countryCode": {
            "stringValue": "91"
          }
        }
      }
    }
  ]
}

Upvotes: 2

Related Questions