From 12f78d23cefd5c504726121597805f47b5a1c919 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 2 May 2023 18:08:53 +0200 Subject: [PATCH] Adding app/types/size.go --- app/types/size.go | 35 +++++++++++++++++++++++++++++++++++ app/types/size_test.go | 34 ++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 4 files changed, 72 insertions(+) create mode 100644 app/types/size.go create mode 100644 app/types/size_test.go diff --git a/app/types/size.go b/app/types/size.go new file mode 100644 index 0000000..a39b80d --- /dev/null +++ b/app/types/size.go @@ -0,0 +1,35 @@ +package types + +import ( + "github.com/docker/go-units" + "gopkg.in/yaml.v3" +) + +// Size is an alias of int64 that can handle sizes represented +// in human readable strings like "200mb", "20 GB" etc + +type Size int64 // Size in bytes. + +// Parse a string into number of bytes stored in a int64 +func (s *Size) Parse(value string) error { + // Empty strings are not an error, they represents zero bytes. + if len(value) < 1 { + *s = 0 + return nil + } + + v, err := units.FromHumanSize(value) + if err != nil { + return err + } + *s = Size(v) + return nil +} + +func (s Size) String() string { + return units.HumanSize(float64(s)) +} + +func (s *Size) UnmarshalYAML(value *yaml.Node) error { + return s.Parse(value.Value) +} diff --git a/app/types/size_test.go b/app/types/size_test.go new file mode 100644 index 0000000..b2cfbe2 --- /dev/null +++ b/app/types/size_test.go @@ -0,0 +1,34 @@ +package types + +import "testing" + +func TestSize_Parse(t *testing.T) { + tests := []struct { + name string + value string + expected int64 + wantErr bool + }{ + {"Empty", "", 0, false}, + {"NoDigit", "abcdefg", 0, true}, + {"Negative", "-10MB", 0, true}, + {"Invalid prefix", "100WAX", 0, true}, + {"Multiple spaces between prefix and value", "100 gb", 0, true}, + {"100kb", "100kb", 100 * 1000, false}, + {"10MB", "10 MB", 10 * 1000 * 1000, false}, + {"2gb", "2gb", 2 * 1000 * 1000 * 1000, false}, + {"4Tb", "4 Tb", 4 * 1000 * 1000 * 1000 * 1000, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := Size(0) + if err := s.Parse(tt.value); (err != nil) != tt.wantErr { + t.Errorf("Size.Parse() error = %v, wantErr %v", err, tt.wantErr) + } + + if int64(s) != tt.expected { + t.Errorf("Size = %v, expected %v", s, tt.expected) + } + }) + } +} diff --git a/go.mod b/go.mod index 04934f6..63a5aa4 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/eosswedenorg/thalos go 1.18 require ( + github.com/docker/go-units v0.5.0 github.com/eoscanada/eos-go v0.10.3-0.20230413154640-bb75101dc2f6 github.com/eosswedenorg-go/antelope-ship-client v0.2.3 github.com/eosswedenorg-go/pid v1.0.1 diff --git a/go.sum b/go.sum index 98e05d4..a7828ee 100644 --- a/go.sum +++ b/go.sum @@ -18,6 +18,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/eoscanada/eos-go v0.10.3-0.20230413154640-bb75101dc2f6 h1:93LUOgAmRkmz8DF2V62GBAFm+7JgWA15zI1uYukBeRk= github.com/eoscanada/eos-go v0.10.3-0.20230413154640-bb75101dc2f6/go.mod h1:L3avCf8OkDrjlUeNy9DdoV67TCmDNj2dSlc5Xp3DNNk= github.com/eosswedenorg-go/antelope-ship-client v0.2.3 h1:08HOQj3YtlEYVsm0RoNZ27JsZWikrUISKAUli6H1Qac=