package config import ( "fmt" "github.com/dustin/go-humanize" ) // Size is a custom type that can unmarshal from both int64 and string type Size int64 // UnmarshalYAML implements yaml.Unmarshaler for Size func (s *Size) UnmarshalYAML(unmarshal func(interface{}) error) error { // Try to unmarshal as int64 first var intVal int64 if err := unmarshal(&intVal); err == nil { *s = Size(intVal) return nil } // Try to unmarshal as string var strVal string if err := unmarshal(&strVal); err != nil { return fmt.Errorf("size must be a number or string") } // Parse the string using go-humanize bytes, err := humanize.ParseBytes(strVal) if err != nil { return fmt.Errorf("invalid size format: %w", err) } *s = Size(bytes) return nil } // Int64 returns the size as int64 func (s Size) Int64() int64 { return int64(s) } // String returns the size as a human-readable string func (s Size) String() string { return humanize.Bytes(uint64(s)) }