goai_shemas.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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) Clone() Schemas {
  24. newSchemas := createSchemas()
  25. newSchemas.refs = s.refs.Clone()
  26. return newSchemas
  27. }
  28. func (s *Schemas) Get(name string) *SchemaRef {
  29. s.init()
  30. value := s.refs.Get(name)
  31. if value != nil {
  32. ref := value.(SchemaRef)
  33. return &ref
  34. }
  35. return nil
  36. }
  37. func (s *Schemas) Set(name string, ref SchemaRef) {
  38. s.init()
  39. s.refs.Set(name, ref)
  40. }
  41. func (s *Schemas) Removes(names []interface{}) {
  42. s.init()
  43. s.refs.Removes(names)
  44. }
  45. func (s *Schemas) Map() map[string]SchemaRef {
  46. s.init()
  47. m := make(map[string]SchemaRef)
  48. s.refs.Iterator(func(key, value interface{}) bool {
  49. m[key.(string)] = value.(SchemaRef)
  50. return true
  51. })
  52. return m
  53. }
  54. func (s *Schemas) Iterator(f func(key string, ref SchemaRef) bool) {
  55. s.init()
  56. s.refs.Iterator(func(key, value interface{}) bool {
  57. return f(key.(string), value.(SchemaRef))
  58. })
  59. }
  60. func (s Schemas) MarshalJSON() ([]byte, error) {
  61. s.init()
  62. return s.refs.MarshalJSON()
  63. }