| 1 |
// Copyright 2024 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 strfmt |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"regexp" |
| 20 |
) |
| 21 |
|
| 22 |
const k8sPrefix = "k8s-" |
| 23 |
|
| 24 |
func init() { |
| 25 |
// register formats in the KubernetesExtensions registry: |
| 26 |
// - k8s-short-name |
| 27 |
// - k8s-long-name |
| 28 |
shortName := ShortName("") |
| 29 |
Default.Add(k8sPrefix+"short-name", &shortName, IsShortName) |
| 30 |
|
| 31 |
longName := LongName("") |
| 32 |
Default.Add(k8sPrefix+"long-name", &longName, IsLongName) |
| 33 |
} |
| 34 |
|
| 35 |
// ShortName is a name, up to 63 characters long, composed of alphanumeric |
| 36 |
// characters and dashes, which cannot begin or end with a dash. |
| 37 |
// |
| 38 |
// ShortName almost conforms to the definition of a label in DNS (RFC 1123), |
| 39 |
// except that uppercase letters are not allowed. |
| 40 |
// |
| 41 |
// xref: https://github.com/kubernetes/kubernetes/issues/71140 |
| 42 |
// |
| 43 |
// swagger:strfmt k8s-short-name |
| 44 |
type ShortName string |
| 45 |
|
| 46 |
func (r ShortName) MarshalText() ([]byte, error) { |
| 47 |
return []byte(string(r)), nil |
| 48 |
} |
| 49 |
|
| 50 |
func (r *ShortName) UnmarshalText(data []byte) error { // validation is performed later on |
| 51 |
*r = ShortName(data) |
| 52 |
return nil |
| 53 |
} |
| 54 |
|
| 55 |
func (r ShortName) String() string { |
| 56 |
return string(r) |
| 57 |
} |
| 58 |
|
| 59 |
func (r ShortName) MarshalJSON() ([]byte, error) { |
| 60 |
return json.Marshal(string(r)) |
| 61 |
} |
| 62 |
|
| 63 |
func (r *ShortName) UnmarshalJSON(data []byte) error { |
| 64 |
return unmarshalJSON(r, data) |
| 65 |
} |
| 66 |
|
| 67 |
func (r *ShortName) DeepCopyInto(out *ShortName) { |
| 68 |
*out = *r |
| 69 |
} |
| 70 |
|
| 71 |
func (r *ShortName) DeepCopy() *ShortName { |
| 72 |
return deepCopy(r) |
| 73 |
} |
| 74 |
|
| 75 |
const shortNameFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?" |
| 76 |
|
| 77 |
// ShortNameMaxLength is a label's max length in DNS (RFC 1123) |
| 78 |
const ShortNameMaxLength int = 63 |
| 79 |
|
| 80 |
var shortNameRegexp = regexp.MustCompile("^" + shortNameFmt + "$") |
| 81 |
|
| 82 |
// IsShortName checks if a string is a valid ShortName. |
| 83 |
func IsShortName(value string) bool { |
| 84 |
return len(value) <= ShortNameMaxLength && |
| 85 |
shortNameRegexp.MatchString(value) |
| 86 |
} |
| 87 |
|
| 88 |
// LongName is a name, up to 253 characters long, composed of dot-separated |
| 89 |
// segments; each segment uses only alphanumerics and dashes (no |
| 90 |
// leading/trailing). |
| 91 |
// |
| 92 |
// LongName almost conforms to the definition of a subdomain in DNS (RFC 1123), |
| 93 |
// except that uppercase letters are not allowed, and there is no max length |
| 94 |
// limit of 63 for each of the dot-separated DNS Labels that make up the |
| 95 |
// subdomain. |
| 96 |
// |
| 97 |
// xref: https://github.com/kubernetes/kubernetes/issues/71140 |
| 98 |
// xref: https://github.com/kubernetes/kubernetes/issues/79351 |
| 99 |
// |
| 100 |
// swagger:strfmt k8s-long-name |
| 101 |
type LongName string |
| 102 |
|
| 103 |
func (r LongName) MarshalText() ([]byte, error) { |
| 104 |
return []byte(string(r)), nil |
| 105 |
} |
| 106 |
|
| 107 |
func (r *LongName) UnmarshalText(data []byte) error { // validation is performed later on |
| 108 |
*r = LongName(data) |
| 109 |
return nil |
| 110 |
} |
| 111 |
|
| 112 |
func (r LongName) String() string { |
| 113 |
return string(r) |
| 114 |
} |
| 115 |
|
| 116 |
func (r LongName) MarshalJSON() ([]byte, error) { |
| 117 |
return json.Marshal(string(r)) |
| 118 |
} |
| 119 |
|
| 120 |
func (r *LongName) UnmarshalJSON(data []byte) error { |
| 121 |
return unmarshalJSON(r, data) |
| 122 |
} |
| 123 |
|
| 124 |
func (r *LongName) DeepCopyInto(out *LongName) { |
| 125 |
*out = *r |
| 126 |
} |
| 127 |
|
| 128 |
func (r *LongName) DeepCopy() *LongName { |
| 129 |
return deepCopy(r) |
| 130 |
} |
| 131 |
|
| 132 |
const longNameFmt string = shortNameFmt + "(\\." + shortNameFmt + ")*" |
| 133 |
|
| 134 |
// LongNameMaxLength is a subdomain's max length in DNS (RFC 1123) |
| 135 |
const LongNameMaxLength int = 253 |
| 136 |
|
| 137 |
var longNameRegexp = regexp.MustCompile("^" + longNameFmt + "$") |
| 138 |
|
| 139 |
// IsLongName checks if a string is a valid LongName. |
| 140 |
func IsLongName(value string) bool { |
| 141 |
return len(value) <= LongNameMaxLength && |
| 142 |
longNameRegexp.MatchString(value) |
| 143 |
} |