OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2012 Matt Jibson <matt.jibson@gmail.com> |
| 3 * |
| 4 * Permission to use, copy, modify, and distribute this software for any |
| 5 * purpose with or without fee is hereby granted, provided that the above |
| 6 * copyright notice and this permission notice appear in all copies. |
| 7 * |
| 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 */ |
| 16 |
| 17 package goon |
| 18 |
| 19 import ( |
| 20 "fmt" |
| 21 "reflect" |
| 22 "strings" |
| 23 |
| 24 "appengine/datastore" |
| 25 |
| 26 "github.com/mjibson/goon" |
| 27 ) |
| 28 |
| 29 // SetStructKey assigns a datastore.Key into a goon-style struct. |
| 30 // Copied from goon since it's internal and we need to populate the completed |
| 31 // key after a Datastore.Put() |
| 32 func SetStructKey(src interface{}, key *datastore.Key, knr goon.KindNameResolver
) error { |
| 33 v := reflect.ValueOf(src) |
| 34 t := v.Type() |
| 35 k := t.Kind() |
| 36 |
| 37 if k != reflect.Ptr { |
| 38 return fmt.Errorf("goon: Expected pointer to struct, got instead
: %v", k) |
| 39 } |
| 40 |
| 41 v = reflect.Indirect(v) |
| 42 t = v.Type() |
| 43 k = t.Kind() |
| 44 |
| 45 if k != reflect.Struct { |
| 46 return fmt.Errorf(fmt.Sprintf("goon: Expected struct, got instea
d: %v", k)) |
| 47 } |
| 48 |
| 49 idSet := false |
| 50 kindSet := false |
| 51 parentSet := false |
| 52 for i := 0; i < v.NumField(); i++ { |
| 53 tf := t.Field(i) |
| 54 vf := v.Field(i) |
| 55 |
| 56 if !vf.CanSet() { |
| 57 continue |
| 58 } |
| 59 |
| 60 tag := tf.Tag.Get("goon") |
| 61 tagValues := strings.Split(tag, ",") |
| 62 if len(tagValues) > 0 { |
| 63 tagValue := tagValues[0] |
| 64 if tagValue == "id" { |
| 65 if idSet { |
| 66 return fmt.Errorf("goon: Only one field
may be marked id") |
| 67 } |
| 68 |
| 69 switch vf.Kind() { |
| 70 case reflect.Int64: |
| 71 vf.SetInt(key.IntID()) |
| 72 idSet = true |
| 73 case reflect.String: |
| 74 vf.SetString(key.StringID()) |
| 75 idSet = true |
| 76 } |
| 77 } else if tagValue == "kind" { |
| 78 if kindSet { |
| 79 return fmt.Errorf("goon: Only one field
may be marked kind") |
| 80 } |
| 81 if vf.Kind() == reflect.String { |
| 82 if (len(tagValues) <= 1 || key.Kind() !=
tagValues[1]) && knr(src) != key.Kind() { |
| 83 vf.Set(reflect.ValueOf(key.Kind(
))) |
| 84 } |
| 85 kindSet = true |
| 86 } |
| 87 } else if tagValue == "parent" { |
| 88 if parentSet { |
| 89 return fmt.Errorf("goon: Only one field
may be marked parent") |
| 90 } |
| 91 if vf.Type() == reflect.TypeOf(&datastore.Key{})
{ |
| 92 vf.Set(reflect.ValueOf(key.Parent())) |
| 93 parentSet = true |
| 94 } |
| 95 } |
| 96 } |
| 97 } |
| 98 |
| 99 if !idSet { |
| 100 return fmt.Errorf("goon: Could not set id field") |
| 101 } |
| 102 |
| 103 return nil |
| 104 } |
OLD | NEW |