1 // Copyright 2011 Google Inc. All rights reserved.
2 // Use of this source code is governed by the Apache 2.0
3 // license that can be found in the LICENSE file.
13 "github.com/golang/protobuf/proto"
14 "google.golang.org/appengine"
15 pb "google.golang.org/appengine/internal/datastore"
19 typeOfBlobKey = reflect.TypeOf(appengine.BlobKey(""))
20 typeOfByteSlice = reflect.TypeOf([]byte(nil))
21 typeOfByteString = reflect.TypeOf(ByteString(nil))
22 typeOfGeoPoint = reflect.TypeOf(appengine.GeoPoint{})
23 typeOfTime = reflect.TypeOf(time.Time{})
24 typeOfKeyPtr = reflect.TypeOf(&Key{})
25 typeOfEntityPtr = reflect.TypeOf(&Entity{})
28 // typeMismatchReason returns a string explaining why the property p could not
29 // be stored in an entity field of type v.Type().
30 func typeMismatchReason(pValue interface{}, v reflect.Value) string {
32 switch pValue.(type) {
42 entityType = "*datastore.Key"
44 entityType = "time.Time"
45 case appengine.BlobKey:
46 entityType = "appengine.BlobKey"
47 case appengine.GeoPoint:
48 entityType = "appengine.GeoPoint"
50 entityType = "datastore.ByteString"
54 return fmt.Sprintf("type mismatch: %s versus %v", entityType, v.Type())
57 type propertyLoader struct {
58 // m holds the number of times a substruct field like "Foo.Bar.Baz" has
59 // been seen so far. The map is constructed lazily.
63 func (l *propertyLoader) load(codec *structCodec, structValue reflect.Value, p Property, requireSlice bool) string {
69 // If name ends with a '.', the last field is anonymous.
70 // In this case, strings.Split will give us "" as the
71 // last element of our fields slice, which will match the ""
72 // field name in the substruct codec.
73 fields := strings.Split(name, ".")
76 var decoder fieldCodec
79 // Cut off the last field (delimited by ".") and find its parent
81 // eg. for name "A.B.C.D", split off "A.B.C" and try to
82 // find a field in the codec with this name.
83 // Loop again with "A.B", etc.
84 for i := len(fields); i > 0; i-- {
85 parent := strings.Join(fields[:i], ".")
86 decoder, ok = codec.fields[parent]
93 // If we never found a matching field in the codec, return
96 return "no such struct field"
99 v = initField(structValue, decoder.path)
101 return "no such struct field"
104 return "cannot set struct field"
107 if decoder.structCodec != nil {
108 codec = decoder.structCodec
112 if v.Kind() == reflect.Slice && v.Type() != typeOfByteSlice {
114 l.m = make(map[string]int)
116 sliceIndex = l.m[p.Name]
117 l.m[p.Name] = sliceIndex + 1
118 for v.Len() <= sliceIndex {
119 v.Set(reflect.Append(v, reflect.New(v.Type().Elem()).Elem()))
121 structValue = v.Index(sliceIndex)
126 var slice reflect.Value
127 if v.Kind() == reflect.Slice && v.Type().Elem().Kind() != reflect.Uint8 {
129 v = reflect.New(v.Type().Elem()).Elem()
130 } else if requireSlice {
131 return "multiple-valued property requires a slice field type"
134 // Convert indexValues to a Go value with a meaning derived from the
137 if iv, ok := pValue.(indexValue); ok {
138 meaning := pb.Property_NO_MEANING
141 meaning = pb.Property_BLOBKEY
142 case typeOfByteSlice:
143 meaning = pb.Property_BLOB
144 case typeOfByteString:
145 meaning = pb.Property_BYTESTRING
147 meaning = pb.Property_GEORSS_POINT
149 meaning = pb.Property_GD_WHEN
150 case typeOfEntityPtr:
151 meaning = pb.Property_ENTITY_PROTO
154 pValue, err = propValue(iv.value, meaning)
160 if errReason := setVal(v, pValue); errReason != "" {
161 // Set the slice back to its zero value.
163 slice.Set(reflect.Zero(slice.Type()))
169 slice.Index(sliceIndex).Set(v)
175 // setVal sets v to the value pValue.
176 func setVal(v reflect.Value, pValue interface{}) string {
178 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
179 x, ok := pValue.(int64)
180 if !ok && pValue != nil {
181 return typeMismatchReason(pValue, v)
183 if v.OverflowInt(x) {
184 return fmt.Sprintf("value %v overflows struct field of type %v", x, v.Type())
188 x, ok := pValue.(bool)
189 if !ok && pValue != nil {
190 return typeMismatchReason(pValue, v)
194 switch x := pValue.(type) {
195 case appengine.BlobKey:
196 v.SetString(string(x))
198 v.SetString(string(x))
203 return typeMismatchReason(pValue, v)
206 case reflect.Float32, reflect.Float64:
207 x, ok := pValue.(float64)
208 if !ok && pValue != nil {
209 return typeMismatchReason(pValue, v)
211 if v.OverflowFloat(x) {
212 return fmt.Sprintf("value %v overflows struct field of type %v", x, v.Type())
216 x, ok := pValue.(*Key)
217 if !ok && pValue != nil {
218 return typeMismatchReason(pValue, v)
220 if _, ok := v.Interface().(*Key); !ok {
221 return typeMismatchReason(pValue, v)
223 v.Set(reflect.ValueOf(x))
227 x, ok := pValue.(time.Time)
228 if !ok && pValue != nil {
229 return typeMismatchReason(pValue, v)
231 v.Set(reflect.ValueOf(x))
233 x, ok := pValue.(appengine.GeoPoint)
234 if !ok && pValue != nil {
235 return typeMismatchReason(pValue, v)
237 v.Set(reflect.ValueOf(x))
239 ent, ok := pValue.(*Entity)
241 return typeMismatchReason(pValue, v)
244 // Recursively load nested struct
245 pls, err := newStructPLS(v.Addr().Interface())
250 // if ent has a Key value and our struct has a Key field,
251 // load the Entity's Key value into the Key field on the struct.
252 if ent.Key != nil && pls.codec.keyField != -1 {
254 pls.v.Field(pls.codec.keyField).Set(reflect.ValueOf(ent.Key))
257 err = pls.Load(ent.Properties)
263 x, ok := pValue.([]byte)
265 if y, yok := pValue.(ByteString); yok {
266 x, ok = []byte(y), true
269 if !ok && pValue != nil {
270 return typeMismatchReason(pValue, v)
272 if v.Type().Elem().Kind() != reflect.Uint8 {
273 return typeMismatchReason(pValue, v)
277 return typeMismatchReason(pValue, v)
282 // initField is similar to reflect's Value.FieldByIndex, in that it
283 // returns the nested struct field corresponding to index, but it
284 // initialises any nil pointers encountered when traversing the structure.
285 func initField(val reflect.Value, index []int) reflect.Value {
286 for _, i := range index[:len(index)-1] {
288 if val.Kind() == reflect.Ptr {
290 val.Set(reflect.New(val.Type().Elem()))
295 return val.Field(index[len(index)-1])
298 // loadEntity loads an EntityProto into PropertyLoadSaver or struct pointer.
299 func loadEntity(dst interface{}, src *pb.EntityProto) (err error) {
300 ent, err := protoToEntity(src)
304 if e, ok := dst.(PropertyLoadSaver); ok {
305 return e.Load(ent.Properties)
307 return LoadStruct(dst, ent.Properties)
310 func (s structPLS) Load(props []Property) error {
311 var fieldName, reason string
313 for _, p := range props {
314 if errStr := l.load(s.codec, s.v, p, p.Multiple); errStr != "" {
315 // We don't return early, as we try to load as many properties as possible.
316 // It is valid to load an entity into a struct that cannot fully represent it.
317 // That case returns an error, but the caller is free to ignore it.
318 fieldName, reason = p.Name, errStr
322 return &ErrFieldMismatch{
323 StructType: s.v.Type(),
324 FieldName: fieldName,
331 func protoToEntity(src *pb.EntityProto) (*Entity, error) {
332 props, rawProps := src.Property, src.RawProperty
333 outProps := make([]Property, 0, len(props)+len(rawProps))
340 x, props = props[0], props[1:]
341 } else if len(rawProps) > 0 {
342 x, rawProps = rawProps[0], rawProps[1:]
348 var value interface{}
349 if x.Meaning != nil && *x.Meaning == pb.Property_INDEX_VALUE {
350 value = indexValue{x.Value}
353 value, err = propValue(x.Value, x.GetMeaning())
358 outProps = append(outProps, Property{
362 Multiple: x.GetMultiple(),
368 // Ignore any error, since nested entity values
369 // are allowed to have an invalid key.
370 key, _ = protoToKey(src.Key)
372 return &Entity{key, outProps}, nil
375 // propValue returns a Go value that combines the raw PropertyValue with a
376 // meaning. For example, an Int64Value with GD_WHEN becomes a time.Time.
377 func propValue(v *pb.PropertyValue, m pb.Property_Meaning) (interface{}, error) {
379 case v.Int64Value != nil:
380 if m == pb.Property_GD_WHEN {
381 return fromUnixMicro(*v.Int64Value), nil
383 return *v.Int64Value, nil
385 case v.BooleanValue != nil:
386 return *v.BooleanValue, nil
387 case v.StringValue != nil:
388 if m == pb.Property_BLOB {
389 return []byte(*v.StringValue), nil
390 } else if m == pb.Property_BLOBKEY {
391 return appengine.BlobKey(*v.StringValue), nil
392 } else if m == pb.Property_BYTESTRING {
393 return ByteString(*v.StringValue), nil
394 } else if m == pb.Property_ENTITY_PROTO {
395 var ent pb.EntityProto
396 err := proto.Unmarshal([]byte(*v.StringValue), &ent)
400 return protoToEntity(&ent)
402 return *v.StringValue, nil
404 case v.DoubleValue != nil:
405 return *v.DoubleValue, nil
406 case v.Referencevalue != nil:
407 key, err := referenceValueToKey(v.Referencevalue)
412 case v.Pointvalue != nil:
413 // NOTE: Strangely, latitude maps to X, longitude to Y.
414 return appengine.GeoPoint{Lat: v.Pointvalue.GetX(), Lng: v.Pointvalue.GetY()}, nil
419 // indexValue is a Property value that is created when entities are loaded from
420 // an index, such as from a projection query.
422 // Such Property values do not contain all of the metadata required to be
423 // faithfully represented as a Go value, and are instead represented as an
424 // opaque indexValue. Load the properties into a concrete struct type (e.g. by
425 // passing a struct pointer to Iterator.Next) to reconstruct actual Go values
426 // of type int, string, time.Time, etc.
427 type indexValue struct {
428 value *pb.PropertyValue