1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-24 10:23:41 +02:00

rename app folder to internal.

This commit is contained in:
Henrik Hautakoski 2024-02-14 13:00:33 +01:00
parent afb90af1db
commit 9974bfe3fd
28 changed files with 23 additions and 23 deletions

36
internal/types/size.go Normal file
View file

@ -0,0 +1,36 @@
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.
// The value is in bytes.
type Size int64
// 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)
}

View file

@ -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)
}
})
}
}