primary_field.go 497 B

12345678910111213141516171819202122232425262728
  1. package login
  2. import (
  3. "fmt"
  4. "github.com/iancoleman/strcase"
  5. "github.com/sunfmin/reflectutils"
  6. )
  7. type PrimaryFielder interface {
  8. PrimaryField() string
  9. }
  10. func primaryField(obj interface{}) string {
  11. f := "ID"
  12. if v, ok := obj.(PrimaryFielder); ok {
  13. f = v.PrimaryField()
  14. }
  15. return f
  16. }
  17. func snakePrimaryField(obj interface{}) string {
  18. return strcase.ToSnake(primaryField(obj))
  19. }
  20. func objectID(obj interface{}) string {
  21. return fmt.Sprint(reflectutils.MustGet(obj, primaryField(obj)))
  22. }