schemaPool.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. }
  37. func newSchemaPool() *schemaPool {
  38. p := &schemaPool{}
  39. p.schemaPoolDocuments = make(map[string]*schemaPoolDocument)
  40. p.standaloneDocument = nil
  41. return p
  42. }
  43. func (p *schemaPool) SetStandaloneDocument(document interface{}) {
  44. p.standaloneDocument = document
  45. }
  46. func (p *schemaPool) GetStandaloneDocument() (document interface{}) {
  47. return p.standaloneDocument
  48. }
  49. func (p *schemaPool) GetDocument(reference gojsonreference.JsonReference) (*schemaPoolDocument, error) {
  50. if internalLogEnabled {
  51. internalLog("Get Document ( %s )", reference.String())
  52. }
  53. var err error
  54. // It is not possible to load anything that is not canonical...
  55. if !reference.IsCanonical() {
  56. return nil, errors.New(formatErrorDescription(
  57. Locale.ReferenceMustBeCanonical(),
  58. ErrorDetails{"reference": reference},
  59. ))
  60. }
  61. refToUrl := reference
  62. refToUrl.GetUrl().Fragment = ""
  63. var spd *schemaPoolDocument
  64. // Try to find the requested document in the pool
  65. for k := range p.schemaPoolDocuments {
  66. if k == refToUrl.String() {
  67. spd = p.schemaPoolDocuments[k]
  68. }
  69. }
  70. if spd != nil {
  71. if internalLogEnabled {
  72. internalLog(" From pool")
  73. }
  74. return spd, nil
  75. }
  76. jsonReferenceLoader := NewReferenceLoader(reference.String())
  77. document, err := jsonReferenceLoader.loadJSON()
  78. if err != nil {
  79. return nil, err
  80. }
  81. spd = &schemaPoolDocument{Document: document}
  82. // add the document to the pool for potential later use
  83. p.schemaPoolDocuments[refToUrl.String()] = spd
  84. return spd, nil
  85. }