resource: support using Quantity as command line value
The Quantity type itself cannot be used because the Set method has the wrong signature. Embedding Quantity inside a new QuantityValue type makes it possible to inherit most of the methods while overriding the Set method.
This commit is contained in:
		| @@ -61,8 +61,32 @@ func (m *Quantity) XXX_DiscardUnknown() { | |||||||
|  |  | ||||||
| var xxx_messageInfo_Quantity proto.InternalMessageInfo | var xxx_messageInfo_Quantity proto.InternalMessageInfo | ||||||
|  |  | ||||||
|  | func (m *QuantityValue) Reset()      { *m = QuantityValue{} } | ||||||
|  | func (*QuantityValue) ProtoMessage() {} | ||||||
|  | func (*QuantityValue) Descriptor() ([]byte, []int) { | ||||||
|  | 	return fileDescriptor_612bba87bd70906c, []int{1} | ||||||
|  | } | ||||||
|  | func (m *QuantityValue) XXX_Unmarshal(b []byte) error { | ||||||
|  | 	return xxx_messageInfo_QuantityValue.Unmarshal(m, b) | ||||||
|  | } | ||||||
|  | func (m *QuantityValue) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { | ||||||
|  | 	return xxx_messageInfo_QuantityValue.Marshal(b, m, deterministic) | ||||||
|  | } | ||||||
|  | func (m *QuantityValue) XXX_Merge(src proto.Message) { | ||||||
|  | 	xxx_messageInfo_QuantityValue.Merge(m, src) | ||||||
|  | } | ||||||
|  | func (m *QuantityValue) XXX_Size() int { | ||||||
|  | 	return xxx_messageInfo_QuantityValue.Size(m) | ||||||
|  | } | ||||||
|  | func (m *QuantityValue) XXX_DiscardUnknown() { | ||||||
|  | 	xxx_messageInfo_QuantityValue.DiscardUnknown(m) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | var xxx_messageInfo_QuantityValue proto.InternalMessageInfo | ||||||
|  |  | ||||||
| func init() { | func init() { | ||||||
| 	proto.RegisterType((*Quantity)(nil), "k8s.io.apimachinery.pkg.api.resource.Quantity") | 	proto.RegisterType((*Quantity)(nil), "k8s.io.apimachinery.pkg.api.resource.Quantity") | ||||||
|  | 	proto.RegisterType((*QuantityValue)(nil), "k8s.io.apimachinery.pkg.api.resource.QuantityValue") | ||||||
| } | } | ||||||
|  |  | ||||||
| func init() { | func init() { | ||||||
| @@ -70,20 +94,21 @@ func init() { | |||||||
| } | } | ||||||
|  |  | ||||||
| var fileDescriptor_612bba87bd70906c = []byte{ | var fileDescriptor_612bba87bd70906c = []byte{ | ||||||
| 	// 237 bytes of a gzipped FileDescriptorProto | 	// 254 bytes of a gzipped FileDescriptorProto | ||||||
| 	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x4c, 0x8e, 0xb1, 0x4e, 0xc3, 0x30, | 	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xf2, 0xcd, 0xb6, 0x28, 0xd6, | ||||||
| 	0x10, 0x40, 0xcf, 0x0b, 0x2a, 0x19, 0x2b, 0x84, 0x10, 0xc3, 0xa5, 0x42, 0x0c, 0x2c, 0xd8, 0x6b, | 	0xcb, 0xcc, 0xd7, 0xcf, 0x2e, 0x4d, 0x4a, 0x2d, 0xca, 0x4b, 0x2d, 0x49, 0x2d, 0xd6, 0x2f, 0x4b, | ||||||
| 	0xc5, 0xc8, 0xce, 0x00, 0x23, 0x5b, 0x92, 0x1e, 0xae, 0x15, 0xd5, 0x8e, 0x2e, 0x36, 0x52, 0xb7, | 	0xcd, 0x4b, 0xc9, 0x2f, 0xd2, 0x87, 0x4a, 0x24, 0x16, 0x64, 0xe6, 0x26, 0x26, 0x67, 0x64, 0xe6, | ||||||
| 	0x8e, 0x8c, 0x1d, 0x19, 0x9b, 0xbf, 0xe9, 0xd8, 0xb1, 0x03, 0x03, 0x31, 0x3f, 0x82, 0xea, 0x36, | 	0xa5, 0x16, 0x55, 0xea, 0x17, 0x64, 0xa7, 0x83, 0x04, 0xf4, 0x8b, 0x52, 0x8b, 0xf3, 0x4b, 0x8b, | ||||||
| 	0x52, 0xb7, 0x7b, 0xef, 0xf4, 0x4e, 0x97, 0xbd, 0xd4, 0xd3, 0x56, 0x1a, 0xa7, 0xea, 0x50, 0x12, | 	0x92, 0x53, 0xf5, 0xd3, 0x53, 0xf3, 0x52, 0x8b, 0x12, 0x4b, 0x52, 0x53, 0xf4, 0x0a, 0x8a, 0xf2, | ||||||
| 	0x5b, 0xf2, 0xd4, 0xaa, 0x4f, 0xb2, 0x33, 0xc7, 0xea, 0xb4, 0x28, 0x1a, 0xb3, 0x28, 0xaa, 0xb9, | 	0x4b, 0xf2, 0x85, 0x54, 0x20, 0xba, 0xf4, 0x90, 0x75, 0xe9, 0x15, 0x64, 0xa7, 0x83, 0x04, 0xf4, | ||||||
| 	0xb1, 0xc4, 0x4b, 0xd5, 0xd4, 0xfa, 0x20, 0x14, 0x53, 0xeb, 0x02, 0x57, 0xa4, 0x34, 0x59, 0xe2, | 	0x60, 0xba, 0xa4, 0x74, 0xd3, 0x33, 0x4b, 0x32, 0x4a, 0x93, 0xf4, 0x92, 0xf3, 0x73, 0xf5, 0xd3, | ||||||
| 	0xc2, 0xd3, 0x4c, 0x36, 0xec, 0xbc, 0x1b, 0xdf, 0x1f, 0x2b, 0x79, 0x5e, 0xc9, 0xa6, 0xd6, 0x07, | 	0xf3, 0xd3, 0xf3, 0xf5, 0xc1, 0x9a, 0x93, 0x4a, 0xd3, 0xc0, 0x3c, 0x30, 0x07, 0xcc, 0x82, 0x18, | ||||||
| 	0x21, 0x87, 0xea, 0xf6, 0x51, 0x1b, 0x3f, 0x0f, 0xa5, 0xac, 0xdc, 0x42, 0x69, 0xa7, 0x9d, 0x4a, | 	0xaa, 0x64, 0xc1, 0xc5, 0x11, 0x58, 0x9a, 0x98, 0x57, 0x92, 0x59, 0x52, 0x29, 0x24, 0xc6, 0xc5, | ||||||
| 	0x71, 0x19, 0x3e, 0x12, 0x25, 0x48, 0xd3, 0xf1, 0xe8, 0xdd, 0x34, 0x1b, 0xbd, 0x86, 0xc2, 0x7a, | 	0x56, 0x5c, 0x52, 0x94, 0x99, 0x97, 0x2e, 0xc1, 0xa8, 0xc0, 0xa8, 0xc1, 0x19, 0x04, 0xe5, 0x59, | ||||||
| 	0xe3, 0x97, 0xe3, 0xeb, 0xec, 0xa2, 0xf5, 0x6c, 0xac, 0xbe, 0x11, 0x13, 0xf1, 0x70, 0xf9, 0x76, | 	0x89, 0xcc, 0x58, 0x20, 0xcf, 0xd0, 0xb1, 0x50, 0x9e, 0x61, 0xc2, 0x42, 0x79, 0x86, 0x05, 0x0b, | ||||||
| 	0xa2, 0xa7, 0xab, 0xef, 0x4d, 0x0e, 0x5f, 0x5d, 0x0e, 0xeb, 0x2e, 0x87, 0x4d, 0x97, 0xc3, 0xea, | 	0xe5, 0x19, 0x1a, 0xee, 0x28, 0x30, 0x28, 0xd9, 0x72, 0xf1, 0xc2, 0x74, 0x86, 0x25, 0xe6, 0x94, | ||||||
| 	0x67, 0x02, 0xcf, 0x72, 0xdb, 0x23, 0xec, 0x7a, 0x84, 0x7d, 0x8f, 0xb0, 0x8a, 0x28, 0xb6, 0x11, | 	0xa6, 0x92, 0xa6, 0xdd, 0x49, 0xef, 0xc4, 0x43, 0x39, 0x86, 0x0b, 0x0f, 0xe5, 0x18, 0x6e, 0x3c, | ||||||
| 	0xc5, 0x2e, 0xa2, 0xd8, 0x47, 0x14, 0xbf, 0x11, 0xc5, 0xfa, 0x0f, 0xe1, 0x7d, 0x34, 0x3c, 0xf6, | 	0x94, 0x63, 0x68, 0x78, 0x24, 0xc7, 0x78, 0xe2, 0x91, 0x1c, 0xe3, 0x85, 0x47, 0x72, 0x8c, 0x37, | ||||||
| 	0x1f, 0x00, 0x00, 0xff, 0xff, 0x3c, 0x08, 0x88, 0x49, 0x0e, 0x01, 0x00, 0x00, | 	0x1e, 0xc9, 0x31, 0x3e, 0x78, 0x24, 0xc7, 0x38, 0xe1, 0xb1, 0x1c, 0x43, 0x14, 0x07, 0xcc, 0x5f, | ||||||
|  | 	0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0x21, 0x76, 0x9f, 0x66, 0x4d, 0x01, 0x00, 0x00, | ||||||
| } | } | ||||||
|   | |||||||
| @@ -86,3 +86,15 @@ message Quantity { | |||||||
|   optional string string = 1; |   optional string string = 1; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // QuantityValue makes it possible to use a Quantity as value for a command | ||||||
|  | // line parameter. | ||||||
|  | // | ||||||
|  | // +protobuf=true | ||||||
|  | // +protobuf.embed=string | ||||||
|  | // +protobuf.options.marshal=false | ||||||
|  | // +protobuf.options.(gogoproto.goproto_stringer)=false | ||||||
|  | // +k8s:deepcopy-gen=true | ||||||
|  | message QuantityValue { | ||||||
|  |   optional string string = 1; | ||||||
|  | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -764,3 +764,30 @@ func (q *Quantity) SetScaled(value int64, scale Scale) { | |||||||
| 	q.d.Dec = nil | 	q.d.Dec = nil | ||||||
| 	q.i = int64Amount{value: value, scale: scale} | 	q.i = int64Amount{value: value, scale: scale} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // QuantityValue makes it possible to use a Quantity as value for a command | ||||||
|  | // line parameter. | ||||||
|  | // | ||||||
|  | // +protobuf=true | ||||||
|  | // +protobuf.embed=string | ||||||
|  | // +protobuf.options.marshal=false | ||||||
|  | // +protobuf.options.(gogoproto.goproto_stringer)=false | ||||||
|  | // +k8s:deepcopy-gen=true | ||||||
|  | type QuantityValue struct { | ||||||
|  | 	Quantity | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Set implements pflag.Value.Set and Go flag.Value.Set. | ||||||
|  | func (q *QuantityValue) Set(s string) error { | ||||||
|  | 	quantity, err := ParseQuantity(s) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	q.Quantity = quantity | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Type implements pflag.Value.Type. | ||||||
|  | func (q QuantityValue) Type() string { | ||||||
|  | 	return "quantity" | ||||||
|  | } | ||||||
|   | |||||||
| @@ -21,11 +21,13 @@ import ( | |||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"math" | 	"math" | ||||||
| 	"math/rand" | 	"math/rand" | ||||||
|  | 	"os" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"testing" | 	"testing" | ||||||
| 	"unicode" | 	"unicode" | ||||||
|  |  | ||||||
| 	fuzz "github.com/google/gofuzz" | 	fuzz "github.com/google/gofuzz" | ||||||
|  | 	"github.com/spf13/pflag" | ||||||
|  |  | ||||||
| 	inf "gopkg.in/inf.v0" | 	inf "gopkg.in/inf.v0" | ||||||
| ) | ) | ||||||
| @@ -1475,3 +1477,42 @@ func BenchmarkQuantityAsApproximateFloat64(b *testing.B) { | |||||||
| 	} | 	} | ||||||
| 	b.StopTimer() | 	b.StopTimer() | ||||||
| } | } | ||||||
|  |  | ||||||
|  | var _ pflag.Value = &QuantityValue{} | ||||||
|  |  | ||||||
|  | func TestQuantityValueSet(t *testing.T) { | ||||||
|  | 	q := QuantityValue{} | ||||||
|  |  | ||||||
|  | 	if err := q.Set("invalid"); err == nil { | ||||||
|  |  | ||||||
|  | 		t.Error("'invalid' did not trigger a parse error") | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if err := q.Set("1Mi"); err != nil { | ||||||
|  | 		t.Errorf("parsing 1Mi should have worked, got: %v", err) | ||||||
|  | 	} | ||||||
|  | 	if q.Value() != 1024*1024 { | ||||||
|  | 		t.Errorf("quantity should have been set to 1Mi, got: %v", q) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	data, err := json.Marshal(q) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Errorf("unexpected encoding error: %v", err) | ||||||
|  | 	} | ||||||
|  | 	expected := `"1Mi"` | ||||||
|  | 	if string(data) != expected { | ||||||
|  | 		t.Errorf("expected 1Mi value to be encoded as %q, got: %q", expected, string(data)) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func ExampleQuantityValue() { | ||||||
|  | 	q := QuantityValue{ | ||||||
|  | 		Quantity: MustParse("1Mi"), | ||||||
|  | 	} | ||||||
|  | 	fs := pflag.FlagSet{} | ||||||
|  | 	fs.SetOutput(os.Stdout) | ||||||
|  | 	fs.Var(&q, "mem", "sets amount of memory") | ||||||
|  | 	fs.PrintDefaults() | ||||||
|  | 	// Output: | ||||||
|  | 	// --mem quantity   sets amount of memory (default 1Mi) | ||||||
|  | } | ||||||
|   | |||||||
| @@ -26,3 +26,20 @@ func (in *Quantity) DeepCopyInto(out *Quantity) { | |||||||
| 	*out = in.DeepCopy() | 	*out = in.DeepCopy() | ||||||
| 	return | 	return | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. | ||||||
|  | func (in *QuantityValue) DeepCopyInto(out *QuantityValue) { | ||||||
|  | 	*out = *in | ||||||
|  | 	out.Quantity = in.Quantity.DeepCopy() | ||||||
|  | 	return | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new QuantityValue. | ||||||
|  | func (in *QuantityValue) DeepCopy() *QuantityValue { | ||||||
|  | 	if in == nil { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  | 	out := new(QuantityValue) | ||||||
|  | 	in.DeepCopyInto(out) | ||||||
|  | 	return out | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Patrick Ohly
					Patrick Ohly