builder.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package builder
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. )
  7. const dbTag = "db"
  8. // RawFieldNames converts golang struct field into slice string.
  9. func RawFieldNames(in any, postgresSql ...bool) []string {
  10. out := make([]string, 0)
  11. v := reflect.ValueOf(in)
  12. if v.Kind() == reflect.Ptr {
  13. v = v.Elem()
  14. }
  15. var pg bool
  16. if len(postgresSql) > 0 {
  17. pg = postgresSql[0]
  18. }
  19. // we only accept structs
  20. if v.Kind() != reflect.Struct {
  21. panic(fmt.Errorf("ToMap only accepts structs; got %T", v))
  22. }
  23. typ := v.Type()
  24. for i := 0; i < v.NumField(); i++ {
  25. // gets us a StructField
  26. fi := typ.Field(i)
  27. tagv := fi.Tag.Get(dbTag)
  28. switch tagv {
  29. case "-":
  30. continue
  31. case "":
  32. if pg {
  33. out = append(out, fi.Name)
  34. } else {
  35. out = append(out, fmt.Sprintf("`%s`", fi.Name))
  36. }
  37. default:
  38. // get tag name with the tag opton, e.g.:
  39. // `db:"id"`
  40. // `db:"id,type=char,length=16"`
  41. // `db:",type=char,length=16"`
  42. // `db:"-,type=char,length=16"`
  43. if strings.Contains(tagv, ",") {
  44. tagv = strings.TrimSpace(strings.Split(tagv, ",")[0])
  45. }
  46. if tagv == "-" {
  47. continue
  48. }
  49. if len(tagv) == 0 {
  50. tagv = fi.Name
  51. }
  52. if pg {
  53. out = append(out, tagv)
  54. } else {
  55. out = append(out, fmt.Sprintf("`%s`", tagv))
  56. }
  57. }
  58. }
  59. return out
  60. }
  61. // PostgreSqlJoin concatenates the given elements into a string.
  62. func PostgreSqlJoin(elems []string) string {
  63. b := new(strings.Builder)
  64. for index, e := range elems {
  65. b.WriteString(fmt.Sprintf("%s = $%d, ", e, index+2))
  66. }
  67. if b.Len() == 0 {
  68. return b.String()
  69. }
  70. return b.String()[0 : b.Len()-2]
  71. }