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