...

Source file src/github.com/go-openapi/spec/tag.go

Documentation: github.com/go-openapi/spec

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

View as plain text