goai_shemas.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package goai
  7. import (
  8. "github.com/gogf/gf/v2/container/gmap"
  9. )
  10. type Schemas struct {
  11. refs *gmap.ListMap // map[string]SchemaRef
  12. }
  13. func createSchemas() Schemas {
  14. return Schemas{
  15. refs: gmap.NewListMap(),
  16. }
  17. }
  18. func (s *Schemas) init() {
  19. if s.refs == nil {
  20. s.refs = gmap.NewListMap()
  21. }
  22. }
  23. func (s *Schemas) Get(name string) *SchemaRef {
  24. s.init()
  25. value := s.refs.Get(name)
  26. if value != nil {
  27. ref := value.(SchemaRef)
  28. return &ref
  29. }
  30. return nil
  31. }
  32. func (s *Schemas) Set(name string, ref SchemaRef) {
  33. s.init()
  34. s.refs.Set(name, ref)
  35. }
  36. func (s *Schemas) Removes(names []interface{}) {
  37. s.init()
  38. s.refs.Removes(names)
  39. }
  40. func (s *Schemas) Map() map[string]SchemaRef {
  41. s.init()
  42. m := make(map[string]SchemaRef)
  43. s.refs.Iterator(func(key, value interface{}) bool {
  44. m[key.(string)] = value.(SchemaRef)
  45. return true
  46. })
  47. return m
  48. }
  49. func (s *Schemas) Iterator(f func(key string, ref SchemaRef) bool) {
  50. s.init()
  51. s.refs.Iterator(func(key, value interface{}) bool {
  52. return f(key.(string), value.(SchemaRef))
  53. })
  54. }
  55. func (s Schemas) MarshalJSON() ([]byte, error) {
  56. s.init()
  57. return s.refs.MarshalJSON()
  58. }