Skip to content

Commit

Permalink
resource: support using Quantity as command line value
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pohly committed Oct 8, 2021
1 parent dd650bd commit 963d3c1
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 16 deletions.
57 changes: 41 additions & 16 deletions staging/src/k8s.io/apimachinery/pkg/api/resource/generated.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/api/resource/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,3 +764,30 @@ func (q *Quantity) SetScaled(value int64, scale Scale) {
q.d.Dec = nil
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"
}
41 changes: 41 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/api/resource/quantity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
"fmt"
"math"
"math/rand"
"os"
"strings"
"testing"
"unicode"

fuzz "github.com/google/gofuzz"
"github.com/spf13/pflag"

inf "gopkg.in/inf.v0"
)
Expand Down Expand Up @@ -1475,3 +1477,42 @@ func BenchmarkQuantityAsApproximateFloat64(b *testing.B) {
}
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)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 963d3c1

Please sign in to comment.