k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/spec/tag.go (about)

     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/swag"
    21  	"k8s.io/kube-openapi/pkg/internal"
    22  	jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
    23  )
    24  
    25  // TagProps describe a tag entry in the top level tags section of a swagger spec
    26  type TagProps struct {
    27  	Description  string                 `json:"description,omitempty"`
    28  	Name         string                 `json:"name,omitempty"`
    29  	ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
    30  }
    31  
    32  // Tag allows adding meta data to a single tag that is used by the
    33  // [Operation Object](http://goo.gl/8us55a#operationObject).
    34  // It is not mandatory to have a Tag Object per tag used there.
    35  //
    36  // For more information: http://goo.gl/8us55a#tagObject
    37  type Tag struct {
    38  	VendorExtensible
    39  	TagProps
    40  }
    41  
    42  // MarshalJSON marshal this to JSON
    43  func (t Tag) MarshalJSON() ([]byte, error) {
    44  	if internal.UseOptimizedJSONMarshaling {
    45  		return internal.DeterministicMarshal(t)
    46  	}
    47  	b1, err := json.Marshal(t.TagProps)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	b2, err := json.Marshal(t.VendorExtensible)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	return swag.ConcatJSON(b1, b2), nil
    56  }
    57  
    58  func (t Tag) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
    59  	var x struct {
    60  		Extensions
    61  		TagProps
    62  	}
    63  	x.Extensions = internal.SanitizeExtensions(t.Extensions)
    64  	x.TagProps = t.TagProps
    65  	return opts.MarshalNext(enc, x)
    66  }
    67  
    68  // UnmarshalJSON marshal this from JSON
    69  func (t *Tag) UnmarshalJSON(data []byte) error {
    70  	if internal.UseOptimizedJSONUnmarshaling {
    71  		return jsonv2.Unmarshal(data, t)
    72  	}
    73  
    74  	if err := json.Unmarshal(data, &t.TagProps); err != nil {
    75  		return err
    76  	}
    77  	return json.Unmarshal(data, &t.VendorExtensible)
    78  }
    79  
    80  func (t *Tag) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
    81  	var x struct {
    82  		Extensions
    83  		TagProps
    84  	}
    85  	if err := opts.UnmarshalNext(dec, &x); err != nil {
    86  		return err
    87  	}
    88  	t.Extensions = internal.SanitizeExtensions(x.Extensions)
    89  	t.TagProps = x.TagProps
    90  	return nil
    91  }