1// Copyright 2015 go-swagger maintainers
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
15package spec
16
17/*
18
19import (
20	"net/url"
21	"os"
22	"path"
23	"path/filepath"
24
25	"github.com/go-openapi/jsonpointer"
26)
27
28  // Some currently unused functions and definitions that
29  // used to be part of the expander.
30
31  // Moved here for the record and possible future reuse
32
33var (
34	idPtr, _  = jsonpointer.New("/id")
35	refPtr, _ = jsonpointer.New("/$ref")
36)
37
38func idFromNode(node interface{}) (*Ref, error) {
39	if idValue, _, err := idPtr.Get(node); err == nil {
40		if refStr, ok := idValue.(string); ok && refStr != "" {
41			idRef, err := NewRef(refStr)
42			if err != nil {
43				return nil, err
44			}
45			return &idRef, nil
46		}
47	}
48	return nil, nil
49}
50
51func nextRef(startingNode interface{}, startingRef *Ref, ptr *jsonpointer.Pointer) *Ref {
52	if startingRef == nil {
53		return nil
54	}
55
56	if ptr == nil {
57		return startingRef
58	}
59
60	ret := startingRef
61	var idRef *Ref
62	node := startingNode
63
64	for _, tok := range ptr.DecodedTokens() {
65		node, _, _ = jsonpointer.GetForToken(node, tok)
66		if node == nil {
67			break
68		}
69
70		idRef, _ = idFromNode(node)
71		if idRef != nil {
72			nw, err := ret.Inherits(*idRef)
73			if err != nil {
74				break
75			}
76			ret = nw
77		}
78
79		refRef, _, _ := refPtr.Get(node)
80		if refRef != nil {
81			var rf Ref
82			switch value := refRef.(type) {
83			case string:
84				rf, _ = NewRef(value)
85			}
86			nw, err := ret.Inherits(rf)
87			if err != nil {
88				break
89			}
90			nwURL := nw.GetURL()
91			if nwURL.Scheme == "file" || (nwURL.Scheme == "" && nwURL.Host == "") {
92				nwpt := filepath.ToSlash(nwURL.Path)
93				if filepath.IsAbs(nwpt) {
94					_, err := os.Stat(nwpt)
95					if err != nil {
96						nwURL.Path = filepath.Join(".", nwpt)
97					}
98				}
99			}
100
101			ret = nw
102		}
103
104	}
105
106	return ret
107}
108
109// basePathFromSchemaID returns a new basePath based on an existing basePath and a schema ID
110func basePathFromSchemaID(oldBasePath, id string) string {
111	u, err := url.Parse(oldBasePath)
112	if err != nil {
113		panic(err)
114	}
115	uid, err := url.Parse(id)
116	if err != nil {
117		panic(err)
118	}
119
120	if path.IsAbs(uid.Path) {
121		return id
122	}
123	u.Path = path.Join(path.Dir(u.Path), uid.Path)
124	return u.String()
125}
126*/
127
128// type ExtraSchemaProps map[string]interface{}
129
130// // JSONSchema represents a structure that is a json schema draft 04
131// type JSONSchema struct {
132// 	SchemaProps
133// 	ExtraSchemaProps
134// }
135
136// // MarshalJSON marshal this to JSON
137// func (s JSONSchema) MarshalJSON() ([]byte, error) {
138// 	b1, err := json.Marshal(s.SchemaProps)
139// 	if err != nil {
140// 		return nil, err
141// 	}
142// 	b2, err := s.Ref.MarshalJSON()
143// 	if err != nil {
144// 		return nil, err
145// 	}
146// 	b3, err := s.Schema.MarshalJSON()
147// 	if err != nil {
148// 		return nil, err
149// 	}
150// 	b4, err := json.Marshal(s.ExtraSchemaProps)
151// 	if err != nil {
152// 		return nil, err
153// 	}
154// 	return swag.ConcatJSON(b1, b2, b3, b4), nil
155// }
156
157// // UnmarshalJSON marshal this from JSON
158// func (s *JSONSchema) UnmarshalJSON(data []byte) error {
159// 	var sch JSONSchema
160// 	if err := json.Unmarshal(data, &sch.SchemaProps); err != nil {
161// 		return err
162// 	}
163// 	if err := json.Unmarshal(data, &sch.Ref); err != nil {
164// 		return err
165// 	}
166// 	if err := json.Unmarshal(data, &sch.Schema); err != nil {
167// 		return err
168// 	}
169// 	if err := json.Unmarshal(data, &sch.ExtraSchemaProps); err != nil {
170// 		return err
171// 	}
172// 	*s = sch
173// 	return nil
174// }
175