schemaPool.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 Defines resources pooling.
  22. // Eases referencing and avoids downloading the same resource twice.
  23. //
  24. // created 26-02-2013
  25. package gojsonschema
  26. import (
  27. "errors"
  28. "github.com/xeipuuv/gojsonreference"
  29. )
  30. type schemaPoolDocument struct {
  31. Document interface{}
  32. }
  33. type schemaPool struct {
  34. schemaPoolDocuments map[string]*schemaPoolDocument
  35. standaloneDocument interface{}
  36. jsonLoaderFactory JSONLoaderFactory
  37. }
  38. func newSchemaPool(f JSONLoaderFactory) *schemaPool {
  39. p := &schemaPool{}
  40. p.schemaPoolDocuments = make(map[string]*schemaPoolDocument)
  41. p.standaloneDocument = nil
  42. p.jsonLoaderFactory = f
  43. return p
  44. }
  45. func (p *schemaPool) SetStandaloneDocument(document interface{}) {
  46. p.standaloneDocument = document
  47. }
  48. func (p *schemaPool) GetStandaloneDocument() (document interface{}) {
  49. return p.standaloneDocument
  50. }
  51. func (p *schemaPool) GetDocument(reference gojsonreference.JsonReference) (*schemaPoolDocument, error) {
  52. if internalLogEnabled {
  53. internalLog("Get Document ( %s )", reference.String())
  54. }
  55. var err error
  56. // It is not possible to load anything that is not canonical...
  57. if !reference.IsCanonical() {
  58. return nil, errors.New(formatErrorDescription(
  59. Locale.ReferenceMustBeCanonical(),
  60. ErrorDetails{"reference": reference},
  61. ))
  62. }
  63. refToUrl := reference
  64. refToUrl.GetUrl().Fragment = ""
  65. var spd *schemaPoolDocument
  66. // Try to find the requested document in the pool
  67. for k := range p.schemaPoolDocuments {
  68. if k == refToUrl.String() {
  69. spd = p.schemaPoolDocuments[k]
  70. }
  71. }
  72. if spd != nil {
  73. if internalLogEnabled {
  74. internalLog(" From pool")
  75. }
  76. return spd, nil
  77. }
  78. jsonReferenceLoader := p.jsonLoaderFactory.New(reference.String())
  79. document, err := jsonReferenceLoader.LoadJSON()
  80. if err != nil {
  81. return nil, err
  82. }
  83. spd = &schemaPoolDocument{Document: document}
  84. // add the document to the pool for potential later use
  85. p.schemaPoolDocuments[refToUrl.String()] = spd
  86. return spd, nil
  87. }