type.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package boot
  2. import "sort"
  3. type resource struct {
  4. init initFunc
  5. start startFunc
  6. config ResourceConfig
  7. }
  8. func newResource(i initFunc, s startFunc, op ...ConfigResource) Resource {
  9. config := ResourceConfig{}
  10. for _, elem := range op {
  11. elem(&config)
  12. }
  13. return &resource{
  14. init: i,
  15. start: s,
  16. config: config,
  17. }
  18. }
  19. func (i *resource) Init() error {
  20. if i.init != nil {
  21. return i.init()
  22. }
  23. return nil
  24. }
  25. func (i *resource) Start() error {
  26. if i.start != nil {
  27. return i.start()
  28. }
  29. return nil
  30. }
  31. func (i *resource) Config() ResourceConfig {
  32. return i.config
  33. }
  34. type ResourceConfig struct {
  35. Name string
  36. }
  37. func SetResourceName(name string) ConfigResource {
  38. return func(config *ResourceConfig) {
  39. config.Name = name
  40. }
  41. }
  42. func sortResourceType(res []ResourceType) []ResourceType {
  43. if res == nil || len(res) == 0 {
  44. return []ResourceType{}
  45. }
  46. sort.Sort(SortedResource(res))
  47. return res
  48. }
  49. /**
  50. 排序资源
  51. */
  52. type SortedResource []ResourceType
  53. func (s SortedResource) Len() int {
  54. return len(s)
  55. }
  56. func (s SortedResource) Less(i, j int) bool {
  57. return s[i] < s[j]
  58. }
  59. func (s SortedResource) Swap(i, j int) {
  60. s[i], s[j] = s[j], s[i]
  61. }