Skip to content

Commit

Permalink
Add back check and add time.Time as type
Browse files Browse the repository at this point in the history
  • Loading branch information
irisgve committed Sep 23, 2024
1 parent dd97ee8 commit 0b2af62
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions confmap/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package confmap // import "go.opentelemetry.io/collector/confmap"
import (
"context"
"fmt"
"time"

"go.uber.org/zap"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -165,6 +166,9 @@ func NewRetrievedFromYAML(yamlBytes []byte, opts ...RetrievedOption) (*Retrieved
// - []any;
// - map[string]any;
func NewRetrieved(rawConf any, opts ...RetrievedOption) (*Retrieved, error) {
if err := checkRawConfType(rawConf); err != nil {
return nil, err
}
set := retrievedSettings{}
for _, opt := range opts {
opt.apply(&set)
Expand Down Expand Up @@ -226,3 +230,17 @@ func (r *Retrieved) Close(ctx context.Context) error {

// CloseFunc a function equivalent to Retrieved.Close.
type CloseFunc func(context.Context) error

func checkRawConfType(rawConf any) error {
if rawConf == nil {
return nil
}
switch rawConf.(type) {
case int, int32, int64, float32, float64, bool, string, []any, map[string]any, time.Time:
return nil
default:
return fmt.Errorf(
"unsupported type=%T for retrieved config,"+
" ensure that values are wrapped in quotes", rawConf)
}
}
5 changes: 5 additions & 0 deletions confmap/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func TestNewRetrievedWithOptions(t *testing.T) {
assert.Equal(t, want, ret.Close(context.Background()))
}

func TestNewRetrievedUnsupportedType(t *testing.T) {
_, err := NewRetrieved(errors.New("my error"))
require.Error(t, err)
}

func TestNewRetrievedFromYAML(t *testing.T) {
ret, err := NewRetrievedFromYAML([]byte{})
require.NoError(t, err)
Expand Down

0 comments on commit 0b2af62

Please sign in to comment.