reference.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 gojsonreference
  19. // repository-desc An implementation of JSON Reference - Go language
  20. //
  21. // description Main and unique file.
  22. //
  23. // created 26-02-2013
  24. package gojsonreference
  25. import (
  26. "errors"
  27. "github.com/xeipuuv/gojsonpointer"
  28. "net/url"
  29. "path/filepath"
  30. "runtime"
  31. "strings"
  32. )
  33. const (
  34. const_fragment_char = `#`
  35. )
  36. func NewJsonReference(jsonReferenceString string) (JsonReference, error) {
  37. var r JsonReference
  38. err := r.parse(jsonReferenceString)
  39. return r, err
  40. }
  41. type JsonReference struct {
  42. referenceUrl *url.URL
  43. referencePointer gojsonpointer.JsonPointer
  44. HasFullUrl bool
  45. HasUrlPathOnly bool
  46. HasFragmentOnly bool
  47. HasFileScheme bool
  48. HasFullFilePath bool
  49. }
  50. func (r *JsonReference) GetUrl() *url.URL {
  51. return r.referenceUrl
  52. }
  53. func (r *JsonReference) GetPointer() *gojsonpointer.JsonPointer {
  54. return &r.referencePointer
  55. }
  56. func (r *JsonReference) String() string {
  57. if r.referenceUrl != nil {
  58. return r.referenceUrl.String()
  59. }
  60. if r.HasFragmentOnly {
  61. return const_fragment_char + r.referencePointer.String()
  62. }
  63. return r.referencePointer.String()
  64. }
  65. func (r *JsonReference) IsCanonical() bool {
  66. return (r.HasFileScheme && r.HasFullFilePath) || (!r.HasFileScheme && r.HasFullUrl)
  67. }
  68. // "Constructor", parses the given string JSON reference
  69. func (r *JsonReference) parse(jsonReferenceString string) (err error) {
  70. r.referenceUrl, err = url.Parse(jsonReferenceString)
  71. if err != nil {
  72. return
  73. }
  74. refUrl := r.referenceUrl
  75. if refUrl.Scheme != "" && refUrl.Host != "" {
  76. r.HasFullUrl = true
  77. } else {
  78. if refUrl.Path != "" {
  79. r.HasUrlPathOnly = true
  80. } else if refUrl.RawQuery == "" && refUrl.Fragment != "" {
  81. r.HasFragmentOnly = true
  82. }
  83. }
  84. r.HasFileScheme = refUrl.Scheme == "file"
  85. if runtime.GOOS == "windows" {
  86. // on Windows, a file URL may have an extra leading slash, and if it
  87. // doesn't then its first component will be treated as the host by the
  88. // Go runtime
  89. if refUrl.Host == "" && strings.HasPrefix(refUrl.Path, "/") {
  90. r.HasFullFilePath = filepath.IsAbs(refUrl.Path[1:])
  91. } else {
  92. r.HasFullFilePath = filepath.IsAbs(refUrl.Host + refUrl.Path)
  93. }
  94. } else {
  95. r.HasFullFilePath = filepath.IsAbs(refUrl.Path)
  96. }
  97. // invalid json-pointer error means url has no json-pointer fragment. simply ignore error
  98. r.referencePointer, _ = gojsonpointer.NewJsonPointer(refUrl.Fragment)
  99. return
  100. }
  101. // Creates a new reference from a parent and a child
  102. // If the child cannot inherit from the parent, an error is returned
  103. func (r *JsonReference) Inherits(child JsonReference) (*JsonReference, error) {
  104. childUrl := child.GetUrl()
  105. parentUrl := r.GetUrl()
  106. if childUrl == nil {
  107. return nil, errors.New("childUrl is nil!")
  108. }
  109. if parentUrl == nil {
  110. return nil, errors.New("parentUrl is nil!")
  111. }
  112. ref, err := NewJsonReference(parentUrl.ResolveReference(childUrl).String())
  113. if err != nil {
  114. return nil, err
  115. }
  116. return &ref, err
  117. }