(t reflect.Type)
| 62 | ) |
| 63 | |
| 64 | func loadCodec(t reflect.Type) (*structCodec, error) { |
| 65 | codecsMu.RLock() |
| 66 | codec, ok := codecs[t] |
| 67 | codecsMu.RUnlock() |
| 68 | if ok { |
| 69 | return codec, nil |
| 70 | } |
| 71 | |
| 72 | codecsMu.Lock() |
| 73 | defer codecsMu.Unlock() |
| 74 | if codec, ok := codecs[t]; ok { |
| 75 | return codec, nil |
| 76 | } |
| 77 | |
| 78 | codec = &structCodec{ |
| 79 | fieldByName: make(map[string]int), |
| 80 | facetByName: make(map[string]int), |
| 81 | } |
| 82 | |
| 83 | for i, I := 0, t.NumField(); i < I; i++ { |
| 84 | f := t.Field(i) |
| 85 | name, opts := f.Tag.Get("search"), "" |
| 86 | if i := strings.Index(name, ","); i != -1 { |
| 87 | name, opts = name[:i], name[i+1:] |
| 88 | } |
| 89 | ignore := false |
| 90 | if name == "-" { |
| 91 | ignore = true |
| 92 | } else if name == "" { |
| 93 | name = f.Name |
| 94 | } else if !validFieldName(name) { |
| 95 | return nil, fmt.Errorf("search: struct tag has invalid field name: %q", name) |
| 96 | } |
| 97 | facet := opts == "facet" |
| 98 | codec.byIndex = append(codec.byIndex, structTag{name: name, facet: facet, ignore: ignore}) |
| 99 | if facet { |
| 100 | codec.facetByName[name] = i |
| 101 | } else { |
| 102 | codec.fieldByName[name] = i |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | codecs[t] = codec |
| 107 | return codec, nil |
| 108 | } |
| 109 | |
| 110 | // structFLS adapts a struct to be a FieldLoadSaver. |
| 111 | type structFLS struct { |
no test coverage detected
searching dependent graphs…