Config UI: add missing config sections / partial yaml inputs (BC) (#13319)

This commit is contained in:
Michael Geers 2024-06-08 14:35:46 +02:00 • committed by GitHub
parent 62a1e223b6
commit 554ce1db60
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
133 changed files with 4405 additions and 1572 deletions

67
util/encode/encode.go Normal file
View file

@ -0,0 +1,67 @@
package encode
import (
"fmt"
"math"
"time"
)
type encoder struct {
withDuration bool
}
type Encoder interface {
Encode(v any) any
}
type EncoderOption func(*encoder)
// NewEncoder creates a new encoder with the following default conversions:
// - float NaN/Inf are converted to nil
// - zero time.Time are converted to nil
// - durations are converted to seconds using WithDuration()
// - fmt.Stringer are converted to string
// - time.Time are converted to RFC3339 string
func NewEncoder(opt ...EncoderOption) Encoder {
res := new(encoder)
for _, o := range opt {
o(res)
}
return res
}
func WithDuration() EncoderOption {
return func(e *encoder) {
e.withDuration = true
}
}
// Encode provides a consumer-friendly default encoding for any type
func (e encoder) Encode(v any) any {
switch val := v.(type) {
case time.Time:
if val.IsZero() {
return nil
}
return val.Format(time.RFC3339)
case time.Duration:
// must be before stringer to convert to seconds instead of string
if e.withDuration {
return int(val.Seconds())
}
return val
case float64:
if math.IsNaN(val) || math.IsInf(val, 0) {
return nil
}
return math.Round(val*1e3) / 1e3
case fmt.Stringer:
return val.String()
default:
return val
}
}

View file

@ -0,0 +1,18 @@
package encode
import (
"math"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestEncode(t *testing.T) {
enc := NewEncoder(WithDuration())
assert.Equal(t, nil, enc.Encode(time.Time{}))
assert.Equal(t, nil, enc.Encode(math.NaN()))
assert.Equal(t, nil, enc.Encode(math.Inf(0)))
assert.Equal(t, 30, enc.Encode(30*time.Second))
assert.Equal(t, 3.142, enc.Encode(math.Pi))
}