Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: add yaml flag #3069

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions config/yamlflag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package config

import (
"fmt"

"gopkg.in/yaml.v2"
)

type yamlFlag[T any] struct {
Ptr **T
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ouch :)
But I see why

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage looks like

type Config struct {
  OpenTelemetry *otel.Options `yaml:"open-telemetry"`
  ...
}
...
flag.Var(newYamlFlag(&cfg.OpenTelemetry), "open-telemetry", "OpenTelemetry configuration in YAML format, use flow-style for convenience")
...

if o.OpenTelemetry != nil {
...
}

The value is allocated when flag is set and stays nil when it is not, i.e. this allows to tell if flag was provided.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I saw it, all good :)

value string // only for Set
}

func newYamlFlag[T any](ptr **T) *yamlFlag[T] {
return &yamlFlag[T]{Ptr: ptr}
}

func (yf *yamlFlag[T]) Set(value string) error {
var opts T
if err := yaml.Unmarshal([]byte(value), &opts); err != nil {
return fmt.Errorf("failed to parse yaml: %w", err)
}
*yf.Ptr = &opts
yf.value = value
return nil
}

func (yf *yamlFlag[T]) UnmarshalYAML(unmarshal func(interface{}) error) error {
var opts T
if err := unmarshal(&opts); err != nil {
return err
}
*yf.Ptr = &opts
return nil
}

func (yf *yamlFlag[T]) String() string {
if yf == nil {
return ""
}
return yf.value
}
108 changes: 108 additions & 0 deletions config/yamlflag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package config

import (
"testing"

"gopkg.in/yaml.v2"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type yamlFlagTestConfig struct {
Foo string
Bar []string
Baz int
Qux *yamlFlagTestConfig
}

func TestYamlFlag(t *testing.T) {
t.Run("set", func(t *testing.T) {
cfg := struct {
Field *yamlFlagTestConfig
}{}

f := newYamlFlag(&cfg.Field)
v := `{foo: hello, bar: [world, "1"], baz: 2, qux: {baz: 3}}`
err := f.Set(v)

require.NoError(t, err)
assert.Equal(t, "hello", cfg.Field.Foo)
assert.Equal(t, []string{"world", "1"}, cfg.Field.Bar)
assert.Equal(t, 2, cfg.Field.Baz)
assert.Equal(t, 3, cfg.Field.Qux.Baz)

assert.Equal(t, v, f.String())
})

t.Run("set empty", func(t *testing.T) {
cfg := struct {
Field *yamlFlagTestConfig
}{}

f := newYamlFlag(&cfg.Field)
err := f.Set("")

require.NoError(t, err)
assert.Equal(t, &yamlFlagTestConfig{}, cfg.Field)
assert.Equal(t, "", f.String())
})

t.Run("set empty object", func(t *testing.T) {
cfg := struct {
Field *yamlFlagTestConfig
}{}

f := newYamlFlag(&cfg.Field)
err := f.Set("{}")

require.NoError(t, err)
assert.Equal(t, &yamlFlagTestConfig{}, cfg.Field)
assert.Equal(t, "{}", f.String())
})

t.Run("set invalid yaml", func(t *testing.T) {
cfg := struct {
Field *yamlFlagTestConfig
}{}

f := newYamlFlag(&cfg.Field)
v := `This is not a valid YAML`
err := f.Set(v)

assert.Error(t, err)
})

t.Run("unmarshal YAML", func(t *testing.T) {
AlexanderYastrebov marked this conversation as resolved.
Show resolved Hide resolved
cfg := struct {
Field *yamlFlagTestConfig
}{}

err := yaml.Unmarshal([]byte(`
field:
foo: hello
bar:
- world
- "1"
baz: 2
qux:
baz: 3
`), &cfg)

require.NoError(t, err)
assert.Equal(t, "hello", cfg.Field.Foo)
assert.Equal(t, []string{"world", "1"}, cfg.Field.Bar)
assert.Equal(t, 2, cfg.Field.Baz)
assert.Equal(t, 3, cfg.Field.Qux.Baz)
})

t.Run("unmarshal invalid YAML", func(t *testing.T) {
cfg := struct {
Field *yamlFlagTestConfig
}{}

err := yaml.Unmarshal([]byte(`This is not a valid YAML`), &cfg)

assert.Error(t, err)
})
}
Loading