|
| 1 | +/* |
| 2 | + Copyright 2020 The Compose Specification Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package types |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | +) |
| 22 | + |
| 23 | +type LabelFile struct { |
| 24 | + Path string `yaml:"path,omitempty" json:"path,omitempty"` |
| 25 | + Required bool `yaml:"required" json:"required"` |
| 26 | + Format string `yaml:"format,omitempty" json:"format,omitempty"` |
| 27 | +} |
| 28 | + |
| 29 | +// MarshalYAML makes LabelFile implement yaml.Marshaler |
| 30 | +func (e LabelFile) MarshalYAML() (interface{}, error) { |
| 31 | + if e.Required { |
| 32 | + return e.Path, nil |
| 33 | + } |
| 34 | + return map[string]any{ |
| 35 | + "path": e.Path, |
| 36 | + "required": e.Required, |
| 37 | + }, nil |
| 38 | +} |
| 39 | + |
| 40 | +// MarshalJSON makes LabelFile implement json.Marshaler |
| 41 | +func (e *LabelFile) MarshalJSON() ([]byte, error) { |
| 42 | + if e.Required { |
| 43 | + return json.Marshal(e.Path) |
| 44 | + } |
| 45 | + // Pass as a value to avoid re-entering this method and use the default implementation |
| 46 | + return json.Marshal(*e) |
| 47 | +} |
0 commit comments