You will only see the public (readable) repos here until you login.

field.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package orm
  2. import (
  3. "reflect"
  4. "strings"
  5. )
  6. // fieldType is the type we expect
  7. type fieldType reflect.Kind
  8. // Relation or Column defines type of mapping
  9. const (
  10. Relation fieldType = (fieldType)(reflect.Struct)
  11. Column
  12. )
  13. // field holds necessary data on specific field
  14. type field struct {
  15. sf reflect.StructField
  16. t reflect.Type
  17. v reflect.Value
  18. }
  19. func (f *field) Make() reflect.Value {
  20. o := reflect.New(f.MakeType()).Elem()
  21. o.Set(reflect.MakeMap(o.Type()))
  22. return o
  23. }
  24. func (f *field) MakeType() reflect.Type {
  25. return f.sf.Type
  26. }
  27. // getFieldName returns the field name within the struct,
  28. // e.g struct { fieldName fieldType }{}
  29. func (f *field) getFieldName() string {
  30. return f.sf.Name
  31. }
  32. // getFieldType returns the field type within the struct,
  33. // e.g struct { fieldName fieldType }{}
  34. func (f *field) getFieldType() string {
  35. return f.t.Name()
  36. }
  37. // getType returns the type; Relation or Column
  38. func (f *field) getType() fieldType {
  39. return (fieldType)(f.t.Kind())
  40. }
  41. // getKind returns the actual reflect.Kind
  42. func (f *field) getKind() reflect.Kind {
  43. return f.t.Kind()
  44. }
  45. // getTag returns tags on this field, can be
  46. // db:"siglevalue" (bool type?) or db:"key:value"
  47. func (f *field) getTag(key string) (string, bool) {
  48. if tag := f.sf.Tag.Get(Prefix); len(tag) != 0 {
  49. tags := strings.Split(tag, ";")
  50. for _, tag := range tags {
  51. kv := strings.Split(tag, ":")
  52. kv[0] = strings.Trim(kv[0], " ")
  53. if len(kv) == 1 && kv[0] == key {
  54. return kv[0], true
  55. }
  56. if len(kv) == 2 && kv[0] == key {
  57. kv[1] = strings.Trim(kv[1], " ")
  58. return kv[1], true
  59. }
  60. }
  61. }
  62. return "", false
  63. }
  64. // hasTags checks if it has all keys
  65. func (f *field) hasTags(keys ...string) bool {
  66. match := 0
  67. for _, key := range keys {
  68. if f.hasTag(key) {
  69. match += 1
  70. }
  71. }
  72. return len(keys) == match
  73. }
  74. // hasTags checks if it has key
  75. func (f *field) hasTag(key string) bool {
  76. if _, ok := f.getTag(key); ok {
  77. return true
  78. }
  79. return false
  80. }