schemaReferencePool.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2015 xeipuuv ( https://github.com/xeipuuv )
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // author xeipuuv
  15. // author-github https://github.com/xeipuuv
  16. // author-mail [email protected]
  17. //
  18. // repository-name gojsonschema
  19. // repository-desc An implementation of JSON Schema, based on IETF's draft v4 - Go language.
  20. //
  21. // description Pool of referenced schemas.
  22. //
  23. // created 25-06-2013
  24. package gojsonschema
  25. import (
  26. "fmt"
  27. )
  28. type schemaReferencePool struct {
  29. documents map[string]*subSchema
  30. }
  31. func newSchemaReferencePool() *schemaReferencePool {
  32. p := &schemaReferencePool{}
  33. p.documents = make(map[string]*subSchema)
  34. return p
  35. }
  36. func (p *schemaReferencePool) Get(ref string) (r *subSchema, o bool) {
  37. if internalLogEnabled {
  38. internalLog(fmt.Sprintf("Schema Reference ( %s )", ref))
  39. }
  40. if sch, ok := p.documents[ref]; ok {
  41. if internalLogEnabled {
  42. internalLog(fmt.Sprintf(" From pool"))
  43. }
  44. return sch, true
  45. }
  46. return nil, false
  47. }
  48. func (p *schemaReferencePool) Add(ref string, sch *subSchema) {
  49. if internalLogEnabled {
  50. internalLog(fmt.Sprintf("Add Schema Reference %s to pool", ref))
  51. }
  52. p.documents[ref] = sch
  53. }