| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // HEAVILY adapted from github.com/golang/appengine/datastore | 5 // HEAVILY adapted from github.com/golang/appengine/datastore |
| 6 | 6 |
| 7 package datastore | 7 package datastore |
| 8 | 8 |
| 9 import ( | 9 import ( |
| 10 "fmt" | 10 "fmt" |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 } else if requireSlice { | 135 } else if requireSlice { |
| 136 return "multiple-valued property requires a slice field type" | 136 return "multiple-valued property requires a slice field type" |
| 137 } | 137 } |
| 138 | 138 |
| 139 if ret, ok := doConversion(v); ok { | 139 if ret, ok := doConversion(v); ok { |
| 140 if ret != "" { | 140 if ret != "" { |
| 141 return ret | 141 return ret |
| 142 } | 142 } |
| 143 } else { | 143 } else { |
| 144 knd := v.Kind() | 144 knd := v.Kind() |
| 145 if v.Type().Implements(typeOfKey) { | |
| 146 knd = reflect.Interface | |
| 147 } | |
| 148 | 145 |
| 149 project := PTNull | 146 project := PTNull |
| 150 overflow := (func(interface{}) bool)(nil) | 147 overflow := (func(interface{}) bool)(nil) |
| 151 set := (func(interface{}))(nil) | 148 set := (func(interface{}))(nil) |
| 152 | 149 |
| 153 switch knd { | 150 switch knd { |
| 154 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, re
flect.Int64: | 151 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, re
flect.Int64: |
| 155 project = PTInt | 152 project = PTInt |
| 156 overflow = func(x interface{}) bool { return v.OverflowI
nt(x.(int64)) } | 153 overflow = func(x interface{}) bool { return v.OverflowI
nt(x.(int64)) } |
| 157 set = func(x interface{}) { v.SetInt(x.(int64)) } | 154 set = func(x interface{}) { v.SetInt(x.(int64)) } |
| 158 case reflect.Bool: | 155 case reflect.Bool: |
| 159 project = PTBool | 156 project = PTBool |
| 160 set = func(x interface{}) { v.SetBool(x.(bool)) } | 157 set = func(x interface{}) { v.SetBool(x.(bool)) } |
| 161 case reflect.String: | 158 case reflect.String: |
| 162 project = PTString | 159 project = PTString |
| 163 set = func(x interface{}) { v.SetString(x.(string)) } | 160 set = func(x interface{}) { v.SetString(x.(string)) } |
| 164 case reflect.Float32, reflect.Float64: | 161 case reflect.Float32, reflect.Float64: |
| 165 project = PTFloat | 162 project = PTFloat |
| 166 overflow = func(x interface{}) bool { return v.OverflowF
loat(x.(float64)) } | 163 overflow = func(x interface{}) bool { return v.OverflowF
loat(x.(float64)) } |
| 167 set = func(x interface{}) { v.SetFloat(x.(float64)) } | 164 set = func(x interface{}) { v.SetFloat(x.(float64)) } |
| 168 » » case reflect.Interface: | 165 » » case reflect.Ptr: |
| 169 project = PTKey | 166 project = PTKey |
| 170 set = func(x interface{}) { | 167 set = func(x interface{}) { |
| 171 » » » » if k, ok := x.(Key); ok { | 168 » » » » if k, ok := x.(*Key); ok { |
| 172 v.Set(reflect.ValueOf(k)) | 169 v.Set(reflect.ValueOf(k)) |
| 173 } | 170 } |
| 174 } | 171 } |
| 175 case reflect.Struct: | 172 case reflect.Struct: |
| 176 switch v.Type() { | 173 switch v.Type() { |
| 177 case typeOfTime: | 174 case typeOfTime: |
| 178 project = PTTime | 175 project = PTTime |
| 179 set = func(x interface{}) { v.Set(reflect.ValueO
f(x)) } | 176 set = func(x interface{}) { v.Set(reflect.ValueO
f(x)) } |
| 180 case typeOfGeoPoint: | 177 case typeOfGeoPoint: |
| 181 project = PTGeoPoint | 178 project = PTGeoPoint |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 } | 478 } |
| 482 case reflect.Slice: | 479 case reflect.Slice: |
| 483 if reflect.PtrTo(ft.Elem()).Implements(typeOfPro
pertyConverter) { | 480 if reflect.PtrTo(ft.Elem()).Implements(typeOfPro
pertyConverter) { |
| 484 st.convert = true | 481 st.convert = true |
| 485 } else if ft.Elem().Kind() == reflect.Struct { | 482 } else if ft.Elem().Kind() == reflect.Struct { |
| 486 substructType = ft.Elem() | 483 substructType = ft.Elem() |
| 487 } | 484 } |
| 488 st.isSlice = ft.Elem().Kind() != reflect.Uint8 | 485 st.isSlice = ft.Elem().Kind() != reflect.Uint8 |
| 489 c.hasSlice = c.hasSlice || st.isSlice | 486 c.hasSlice = c.hasSlice || st.isSlice |
| 490 case reflect.Interface: | 487 case reflect.Interface: |
| 491 » » » » if ft != typeOfKey { | 488 » » » » c.problem = me("field %q has non-concrete interf
ace type %s", |
| 492 » » » » » c.problem = me("field %q has non-concret
e interface type %s", | 489 » » » » » f.Name, f.Type) |
| 493 » » » » » » f.Name, f.Type) | 490 » » » » return |
| 494 » » » » » return | |
| 495 » » » » } | |
| 496 } | 491 } |
| 497 } | 492 } |
| 498 | 493 |
| 499 if substructType != nil { | 494 if substructType != nil { |
| 500 sub := getStructCodecLocked(substructType) | 495 sub := getStructCodecLocked(substructType) |
| 501 if sub.problem != nil { | 496 if sub.problem != nil { |
| 502 if sub.problem == errRecursiveStruct { | 497 if sub.problem == errRecursiveStruct { |
| 503 c.problem = me("field %q is recursively
defined", f.Name) | 498 c.problem = me("field %q is recursively
defined", f.Name) |
| 504 } else { | 499 } else { |
| 505 c.problem = me("field %q has problem: %s
", f.Name, sub.problem) | 500 c.problem = me("field %q has problem: %s
", f.Name, sub.problem) |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 switch val { | 568 switch val { |
| 574 case "on", "On", "true": | 569 case "on", "On", "true": |
| 575 return true, nil | 570 return true, nil |
| 576 case "off", "Off", "false": | 571 case "off", "Off", "false": |
| 577 return false, nil | 572 return false, nil |
| 578 } | 573 } |
| 579 return nil, fmt.Errorf("Toggle field has bad/missing default, go
t %q", val) | 574 return nil, fmt.Errorf("Toggle field has bad/missing default, go
t %q", val) |
| 580 } | 575 } |
| 581 return nil, fmt.Errorf("helper: meta field with bad type/value %s/%q", t
, val) | 576 return nil, fmt.Errorf("helper: meta field with bad type/value %s/%q", t
, val) |
| 582 } | 577 } |
| OLD | NEW |