model.go 966 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package client
  2. import (
  3. "time"
  4. )
  5. type Model struct {
  6. ID string
  7. IsCreated bool
  8. IsDeleted bool
  9. IsUpdated bool
  10. LatestUpdatedTime *time.Time
  11. }
  12. func modelFromMap(m map[string]interface{}) *Model {
  13. createdAtStr := ToString(m["created_at"])
  14. updatedAtStr := ToString(m["updated_at"])
  15. deletedAtStr := ToString(m["deleted_at"])
  16. updatedAt, _ := ToTime(updatedAtStr)
  17. isCreated := false
  18. isUpdated := false
  19. isDeleted := false
  20. if createdAtStr == updatedAtStr {
  21. isCreated = true
  22. } else if deletedAtStr != "" {
  23. isDeleted = true
  24. } else {
  25. isUpdated = true
  26. }
  27. return &Model{ID: ToString(m["id"]), LatestUpdatedTime: updatedAt,
  28. IsCreated: isCreated, IsUpdated: isUpdated, IsDeleted: isDeleted}
  29. }
  30. type Product struct {
  31. Model
  32. Name string
  33. Color string
  34. }
  35. func ProductFromMap(m map[string]interface{}) *Product {
  36. return &Product{Model: *modelFromMap(m),
  37. Name: ToString(m["name"]),
  38. Color: ToString(m["color"])}
  39. }