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
17import (
18	"encoding/json"
19
20	"github.com/go-openapi/jsonpointer"
21	"github.com/go-openapi/swag"
22)
23
24// PathItemProps the path item specific properties
25type PathItemProps struct {
26	Get        *Operation  `json:"get,omitempty"`
27	Put        *Operation  `json:"put,omitempty"`
28	Post       *Operation  `json:"post,omitempty"`
29	Delete     *Operation  `json:"delete,omitempty"`
30	Options    *Operation  `json:"options,omitempty"`
31	Head       *Operation  `json:"head,omitempty"`
32	Patch      *Operation  `json:"patch,omitempty"`
33	Parameters []Parameter `json:"parameters,omitempty"`
34}
35
36// PathItem describes the operations available on a single path.
37// A Path Item may be empty, due to [ACL constraints](http://goo.gl/8us55a#securityFiltering).
38// The path itself is still exposed to the documentation viewer but they will
39// not know which operations and parameters are available.
40//
41// For more information: http://goo.gl/8us55a#pathItemObject
42type PathItem struct {
43	Refable
44	VendorExtensible
45	PathItemProps
46}
47
48// JSONLookup look up a value by the json property name
49func (p PathItem) JSONLookup(token string) (interface{}, error) {
50	if ex, ok := p.Extensions[token]; ok {
51		return &ex, nil
52	}
53	if token == jsonRef {
54		return &p.Ref, nil
55	}
56	r, _, err := jsonpointer.GetForToken(p.PathItemProps, token)
57	return r, err
58}
59
60// UnmarshalJSON hydrates this items instance with the data from JSON
61func (p *PathItem) UnmarshalJSON(data []byte) error {
62	if err := json.Unmarshal(data, &p.Refable); err != nil {
63		return err
64	}
65	if err := json.Unmarshal(data, &p.VendorExtensible); err != nil {
66		return err
67	}
68	return json.Unmarshal(data, &p.PathItemProps)
69}
70
71// MarshalJSON converts this items object to JSON
72func (p PathItem) MarshalJSON() ([]byte, error) {
73	b3, err := json.Marshal(p.Refable)
74	if err != nil {
75		return nil, err
76	}
77	b4, err := json.Marshal(p.VendorExtensible)
78	if err != nil {
79		return nil, err
80	}
81	b5, err := json.Marshal(p.PathItemProps)
82	if err != nil {
83		return nil, err
84	}
85	concated := swag.ConcatJSON(b3, b4, b5)
86	return concated, nil
87}
88