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

mappable.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package orm
  2. import (
  3. "strings"
  4. "git.giaever.org/bnb.hosting/orm/conn"
  5. "github.com/go-openapi/inflect"
  6. )
  7. type MappableInterface interface {
  8. SetDB(*conn.DB) MappableInterface
  9. GetDB() *conn.DB
  10. GetTableMapperFn() MapperFn
  11. GetColumnMapperFn() MapperFn
  12. }
  13. type MapperFn func(string) string
  14. type Mappable struct {
  15. db *conn.DB `db:"omit"`
  16. }
  17. func (m *Mappable) SetDB(db *conn.DB) MappableInterface {
  18. m.db = db
  19. return m
  20. }
  21. func (m *Mappable) GetDB() *conn.DB {
  22. return m.db
  23. }
  24. func (m Mappable) GetTableMapperFn() MapperFn {
  25. return func(t string) string {
  26. s := []byte{}
  27. for i := 0; i < len(t); i++ {
  28. c := t[i]
  29. if c >= 'A' && c <= 'Z' {
  30. if i != 0 {
  31. s = append(s, '_')
  32. }
  33. c = c + ('a' - 'A')
  34. }
  35. s = append(s, c)
  36. }
  37. str := strings.Split((string)(s), "_")
  38. str[len(str)-1] = inflect.Pluralize(str[len(str)-1])
  39. return strings.Join(str, "_")
  40. }
  41. }
  42. func (m Mappable) GetColumnMapperFn() MapperFn {
  43. return func(f string) string {
  44. s := []byte{}
  45. for i := 0; i < len(f); i++ {
  46. c := f[i]
  47. if c >= 'A' && c <= 'Z' {
  48. if i != 0 {
  49. s = append(s, '_')
  50. }
  51. c = c + ('a' - 'A')
  52. }
  53. s = append(s, c)
  54. }
  55. return (string)(s)
  56. }
  57. }