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

mappable.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 MappedCollection map[uint64]MappableInterface
  14. type MapperFn func(string) string
  15. type Mappable struct {
  16. db *conn.DB `db:"omit"`
  17. }
  18. func (m *Mappable) SetDB(db *conn.DB) MappableInterface {
  19. m.db = db
  20. return m
  21. }
  22. func (m *Mappable) GetDB() *conn.DB {
  23. return m.db
  24. }
  25. func (m Mappable) GetTableMapperFn() MapperFn {
  26. return func(t string) string {
  27. s := []byte{}
  28. for i := 0; i < len(t); i++ {
  29. c := t[i]
  30. if c >= 'A' && c <= 'Z' {
  31. if i != 0 {
  32. s = append(s, '_')
  33. }
  34. c = c + ('a' - 'A')
  35. }
  36. s = append(s, c)
  37. }
  38. str := strings.Split((string)(s), "_")
  39. str[len(str)-1] = inflect.Pluralize(str[len(str)-1])
  40. return strings.Join(str, "_")
  41. }
  42. }
  43. func (m Mappable) GetColumnMapperFn() MapperFn {
  44. return func(f string) string {
  45. s := []byte{}
  46. for i := 0; i < len(f); i++ {
  47. c := f[i]
  48. if c >= 'A' && c <= 'Z' {
  49. if i != 0 {
  50. s = append(s, '_')
  51. }
  52. c = c + ('a' - 'A')
  53. }
  54. s = append(s, c)
  55. }
  56. return (string)(s)
  57. }
  58. }