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