From 87e598bac270f5b6a12f7719415839c9ca57af5d Mon Sep 17 00:00:00 2001 From: sreeram-venkitesh Date: Mon, 26 May 2025 22:40:43 +0530 Subject: [PATCH] Added quantity format --- pkg/validation/strfmt/quantity.go | 61 ++++++++++++++++++++++++++++++ pkg/validation/validate/helpers.go | 1 + pkg/validation/validate/type.go | 2 + 3 files changed, 64 insertions(+) create mode 100644 pkg/validation/strfmt/quantity.go diff --git a/pkg/validation/strfmt/quantity.go b/pkg/validation/strfmt/quantity.go new file mode 100644 index 000000000..2bddf8ca7 --- /dev/null +++ b/pkg/validation/strfmt/quantity.go @@ -0,0 +1,61 @@ +/* +Copyright 2025 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package strfmt + +import ( + "k8s.io/apimachinery/pkg/api/resource" +) + +type Quantity string + +func init() { + quantity := Quantity("") + Default.Add("quantity", &quantity, isQuantity) +} + +// String converts this value to a string +func (q Quantity) String() string { + return string(q) +} + +// DeepCopyInto copies the receiver into out. out must be non-nil. +func (q *Quantity) DeepCopyInto(out *Quantity) { + *out = *q +} + +// DeepCopy creates a deep copy of Semver +func (q *Quantity) DeepCopy() *Quantity { + if q == nil { + return nil + } + out := new(Quantity) + q.DeepCopyInto(out) + return out +} + +// MarshalText turns this instance into text +func (q Quantity) MarshalText() ([]byte, error) { + return []byte(q), nil +} + +// UnmarshalText hydrates this instance from text +func (q *Quantity) UnmarshalText(data []byte) error { + *(q) = Quantity(data) + return nil +} + +func isQuantity(str string) bool { + _, err := resource.ParseQuantity(str) + return err == nil +} diff --git a/pkg/validation/validate/helpers.go b/pkg/validation/validate/helpers.go index 67514a189..59d4aa50f 100644 --- a/pkg/validation/validate/helpers.go +++ b/pkg/validation/validate/helpers.go @@ -66,6 +66,7 @@ const ( stringFormatUUID3 = "uuid3" stringFormatUUID4 = "uuid4" stringFormatUUID5 = "uuid5" + stringFormatQuantity = "quantity" integerFormatInt32 = "int32" integerFormatInt64 = "int64" diff --git a/pkg/validation/validate/type.go b/pkg/validation/validate/type.go index 6469719dd..55569a7cf 100644 --- a/pkg/validation/validate/type.go +++ b/pkg/validation/validate/type.go @@ -81,6 +81,8 @@ func (t *typeValidator) schemaInfoForType(data interface{}) (string, string) { return stringType, stringFormatUUID4 case strfmt.UUID5, *strfmt.UUID5: return stringType, stringFormatUUID5 + case strfmt.Quantity, *strfmt.Quantity: + return stringType, stringFormatQuantity // TODO: missing binary (io.ReadCloser) // TODO: missing json.Number default: