Serge Bazanski | cc25bdf | 2018-10-25 14:02:58 +0200 | [diff] [blame^] | 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 | |
| 15 | package spec |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | |
| 20 | "github.com/go-openapi/jsonpointer" |
| 21 | "github.com/go-openapi/swag" |
| 22 | ) |
| 23 | |
| 24 | // TagProps describe a tag entry in the top level tags section of a swagger spec |
| 25 | type TagProps struct { |
| 26 | Description string `json:"description,omitempty"` |
| 27 | Name string `json:"name,omitempty"` |
| 28 | ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"` |
| 29 | } |
| 30 | |
| 31 | // NewTag creates a new tag |
| 32 | func NewTag(name, description string, externalDocs *ExternalDocumentation) Tag { |
| 33 | return Tag{TagProps: TagProps{description, name, externalDocs}} |
| 34 | } |
| 35 | |
| 36 | // Tag allows adding meta data to a single tag that is used by the [Operation Object](http://goo.gl/8us55a#operationObject). |
| 37 | // It is not mandatory to have a Tag Object per tag used there. |
| 38 | // |
| 39 | // For more information: http://goo.gl/8us55a#tagObject |
| 40 | type Tag struct { |
| 41 | VendorExtensible |
| 42 | TagProps |
| 43 | } |
| 44 | |
| 45 | // JSONLookup implements an interface to customize json pointer lookup |
| 46 | func (t Tag) JSONLookup(token string) (interface{}, error) { |
| 47 | if ex, ok := t.Extensions[token]; ok { |
| 48 | return &ex, nil |
| 49 | } |
| 50 | |
| 51 | r, _, err := jsonpointer.GetForToken(t.TagProps, token) |
| 52 | return r, err |
| 53 | } |
| 54 | |
| 55 | // MarshalJSON marshal this to JSON |
| 56 | func (t Tag) MarshalJSON() ([]byte, error) { |
| 57 | b1, err := json.Marshal(t.TagProps) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | b2, err := json.Marshal(t.VendorExtensible) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | return swag.ConcatJSON(b1, b2), nil |
| 66 | } |
| 67 | |
| 68 | // UnmarshalJSON marshal this from JSON |
| 69 | func (t *Tag) UnmarshalJSON(data []byte) error { |
| 70 | if err := json.Unmarshal(data, &t.TagProps); err != nil { |
| 71 | return err |
| 72 | } |
| 73 | return json.Unmarshal(data, &t.VendorExtensible) |
| 74 | } |