Chromium Code Reviews| 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 memory | |
| 18 | |
| 19 import ( | |
| 20 "fmt" | |
| 21 "reflect" | |
| 22 "strings" | |
| 23 | |
| 24 "appengine/datastore" | |
| 25 | |
| 26 "github.com/mjibson/goon" | |
| 27 ) | |
| 28 | |
| 29 // copied from goon since it's internal and we need it for Put() :/ | |
|
M-A Ruel
2015/05/25 18:21:09
I'd prefer if it was in its own internal package,
iannucci
2015/05/27 19:33:31
moved to internal/goon
| |
| 30 | |
| 31 func setStructKey(src interface{}, key *datastore.Key, knr goon.KindNameResolver ) error { | |
| 32 v := reflect.ValueOf(src) | |
| 33 t := v.Type() | |
| 34 k := t.Kind() | |
| 35 | |
| 36 if k != reflect.Ptr { | |
| 37 return fmt.Errorf("goon: Expected pointer to struct, got instead : %v", k) | |
| 38 } | |
| 39 | |
| 40 v = reflect.Indirect(v) | |
| 41 t = v.Type() | |
| 42 k = t.Kind() | |
| 43 | |
| 44 if k != reflect.Struct { | |
| 45 return fmt.Errorf(fmt.Sprintf("goon: Expected struct, got instea d: %v", k)) | |
| 46 } | |
| 47 | |
| 48 idSet := false | |
| 49 kindSet := false | |
| 50 parentSet := false | |
| 51 for i := 0; i < v.NumField(); i++ { | |
| 52 tf := t.Field(i) | |
| 53 vf := v.Field(i) | |
| 54 | |
| 55 if !vf.CanSet() { | |
| 56 continue | |
| 57 } | |
| 58 | |
| 59 tag := tf.Tag.Get("goon") | |
| 60 tagValues := strings.Split(tag, ",") | |
| 61 if len(tagValues) > 0 { | |
| 62 tagValue := tagValues[0] | |
| 63 if tagValue == "id" { | |
| 64 if idSet { | |
| 65 return fmt.Errorf("goon: Only one field may be marked id") | |
| 66 } | |
| 67 | |
| 68 switch vf.Kind() { | |
| 69 case reflect.Int64: | |
| 70 vf.SetInt(key.IntID()) | |
| 71 idSet = true | |
| 72 case reflect.String: | |
| 73 vf.SetString(key.StringID()) | |
| 74 idSet = true | |
| 75 } | |
| 76 } else if tagValue == "kind" { | |
| 77 if kindSet { | |
| 78 return fmt.Errorf("goon: Only one field may be marked kind") | |
| 79 } | |
| 80 if vf.Kind() == reflect.String { | |
| 81 if (len(tagValues) <= 1 || key.Kind() != tagValues[1]) && knr(src) != key.Kind() { | |
| 82 vf.Set(reflect.ValueOf(key.Kind( ))) | |
| 83 } | |
| 84 kindSet = true | |
| 85 } | |
| 86 } else if tagValue == "parent" { | |
| 87 if parentSet { | |
| 88 return fmt.Errorf("goon: Only one field may be marked parent") | |
| 89 } | |
| 90 if vf.Type() == reflect.TypeOf(&datastore.Key{}) { | |
| 91 vf.Set(reflect.ValueOf(key.Parent())) | |
| 92 parentSet = true | |
| 93 } | |
| 94 } | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 if !idSet { | |
| 99 return fmt.Errorf("goon: Could not set id field") | |
| 100 } | |
| 101 | |
| 102 return nil | |
| 103 } | |
| OLD | NEW |