getStructCodecLocked implements getStructCodec. The structCodecsMutex must be held when calling this function.
(t reflect.Type)
| 174 | // getStructCodecLocked implements getStructCodec. The structCodecsMutex must |
| 175 | // be held when calling this function. |
| 176 | func getStructCodecLocked(t reflect.Type) (ret *structCodec, retErr error) { |
| 177 | c, ok := structCodecs[t] |
| 178 | if ok { |
| 179 | return c, nil |
| 180 | } |
| 181 | c = &structCodec{ |
| 182 | fields: make(map[string]fieldCodec), |
| 183 | // We initialize keyField to -1 so that the zero-value is not |
| 184 | // misinterpreted as index 0. |
| 185 | keyField: -1, |
| 186 | } |
| 187 | |
| 188 | // Add c to the structCodecs map before we are sure it is good. If t is |
| 189 | // a recursive type, it needs to find the incomplete entry for itself in |
| 190 | // the map. |
| 191 | structCodecs[t] = c |
| 192 | defer func() { |
| 193 | if retErr != nil { |
| 194 | delete(structCodecs, t) |
| 195 | } |
| 196 | }() |
| 197 | |
| 198 | for i := 0; i < t.NumField(); i++ { |
| 199 | f := t.Field(i) |
| 200 | // Skip unexported fields. |
| 201 | // Note that if f is an anonymous, unexported struct field, |
| 202 | // we will promote its fields. |
| 203 | if f.PkgPath != "" && !f.Anonymous { |
| 204 | continue |
| 205 | } |
| 206 | |
| 207 | tags := strings.Split(f.Tag.Get("datastore"), ",") |
| 208 | name := tags[0] |
| 209 | opts := make(map[string]bool) |
| 210 | for _, t := range tags[1:] { |
| 211 | opts[t] = true |
| 212 | } |
| 213 | switch { |
| 214 | case name == "": |
| 215 | if !f.Anonymous { |
| 216 | name = f.Name |
| 217 | } |
| 218 | case name == "-": |
| 219 | continue |
| 220 | case name == "__key__": |
| 221 | if f.Type != typeOfKeyPtr { |
| 222 | return nil, fmt.Errorf("datastore: __key__ field on struct %v is not a *datastore.Key", t) |
| 223 | } |
| 224 | c.keyField = i |
| 225 | case !validPropertyName(name): |
| 226 | return nil, fmt.Errorf("datastore: struct tag has invalid property name: %q", name) |
| 227 | } |
| 228 | |
| 229 | substructType, fIsSlice := reflect.Type(nil), false |
| 230 | switch f.Type.Kind() { |
| 231 | case reflect.Struct: |
| 232 | substructType = f.Type |
| 233 | case reflect.Slice: |
no test coverage detected
searching dependent graphs…