github.com/gogf/gf/v2@v2.7.4/net/goai/goai_shemas.go (about)

     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  
     7  package goai
     8  
     9  import (
    10  	"github.com/gogf/gf/v2/container/gmap"
    11  )
    12  
    13  type Schemas struct {
    14  	refs *gmap.ListMap // map[string]SchemaRef
    15  }
    16  
    17  func createSchemas() *Schemas {
    18  	return &Schemas{
    19  		refs: gmap.NewListMap(),
    20  	}
    21  }
    22  
    23  func (s *Schemas) init() {
    24  	if s.refs == nil {
    25  		s.refs = gmap.NewListMap()
    26  	}
    27  }
    28  
    29  func (s *Schemas) Clone() *Schemas {
    30  	newSchemas := createSchemas()
    31  	newSchemas.refs = s.refs.Clone()
    32  	return newSchemas
    33  }
    34  
    35  func (s *Schemas) Get(name string) *SchemaRef {
    36  	s.init()
    37  	value := s.refs.Get(name)
    38  	if value != nil {
    39  		ref := value.(SchemaRef)
    40  		return &ref
    41  	}
    42  	return nil
    43  }
    44  
    45  func (s *Schemas) Set(name string, ref SchemaRef) {
    46  	s.init()
    47  	s.refs.Set(name, ref)
    48  }
    49  
    50  func (s *Schemas) Removes(names []interface{}) {
    51  	s.init()
    52  	s.refs.Removes(names)
    53  }
    54  
    55  func (s *Schemas) Map() map[string]SchemaRef {
    56  	s.init()
    57  	m := make(map[string]SchemaRef)
    58  	s.refs.Iterator(func(key, value interface{}) bool {
    59  		m[key.(string)] = value.(SchemaRef)
    60  		return true
    61  	})
    62  	return m
    63  }
    64  
    65  func (s *Schemas) Iterator(f func(key string, ref SchemaRef) bool) {
    66  	s.init()
    67  	s.refs.Iterator(func(key, value interface{}) bool {
    68  		return f(key.(string), value.(SchemaRef))
    69  	})
    70  }
    71  
    72  func (s Schemas) MarshalJSON() ([]byte, error) {
    73  	s.init()
    74  	return s.refs.MarshalJSON()
    75  }