parseTableStructTag returns the name of the field according to the `table` struct tag. If the table tag does not exist or is "-", an empty string is returned. If the table tag is malformed, an error is returned. The returned name is transformed from "snake_case" to "normal text".
(field reflect.StructField)
| 303 | // |
| 304 | // The returned name is transformed from "snake_case" to "normal text". |
| 305 | func parseTableStructTag(field reflect.StructField) (name string, defaultSort, noSortOpt, recursive, skipParentName, emptyNil bool, err error) { |
| 306 | tags, err := structtag.Parse(string(field.Tag)) |
| 307 | if err != nil { |
| 308 | return "", false, false, false, false, false, xerrors.Errorf("parse struct field tag %q: %w", string(field.Tag), err) |
| 309 | } |
| 310 | |
| 311 | tag, err := tags.Get("table") |
| 312 | if err != nil || tag.Name == "-" { |
| 313 | // tags.Get only returns an error if the tag is not found. |
| 314 | return "", false, false, false, false, false, nil |
| 315 | } |
| 316 | |
| 317 | defaultSortOpt := false |
| 318 | noSortOpt = false |
| 319 | recursiveOpt := false |
| 320 | skipParentNameOpt := false |
| 321 | emptyNilOpt := false |
| 322 | for _, opt := range tag.Options { |
| 323 | switch opt { |
| 324 | case "default_sort": |
| 325 | defaultSortOpt = true |
| 326 | case "nosort": |
| 327 | noSortOpt = true |
| 328 | case "recursive": |
| 329 | recursiveOpt = true |
| 330 | case "recursive_inline": |
| 331 | // recursive_inline is a helper to make recursive tables look nicer. |
| 332 | // It skips prefixing the parent name to the child name. If you do this, |
| 333 | // make sure the child name is unique across all nested structs in the parent. |
| 334 | recursiveOpt = true |
| 335 | skipParentNameOpt = true |
| 336 | case "empty_nil": |
| 337 | emptyNilOpt = true |
| 338 | default: |
| 339 | return "", false, false, false, false, false, xerrors.Errorf("unknown option %q in struct field tag", opt) |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return strings.ReplaceAll(tag.Name, "_", " "), defaultSortOpt, noSortOpt, recursiveOpt, skipParentNameOpt, emptyNilOpt, nil |
| 344 | } |
| 345 | |
| 346 | func isStructOrStructPointer(t reflect.Type) bool { |
| 347 | return t.Kind() == reflect.Struct || (t.Kind() == reflect.Pointer && t.Elem().Kind() == reflect.Struct) |
no test coverage detected