Skip to content

Commit 0b2af62

Browse files
committed
Add back check and add time.Time as type
1 parent dd97ee8 commit 0b2af62

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

confmap/provider.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package confmap // import "go.opentelemetry.io/collector/confmap"
66
import (
77
"context"
88
"fmt"
9+
"time"
910

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

227231
// CloseFunc a function equivalent to Retrieved.Close.
228232
type CloseFunc func(context.Context) error
233+
234+
func checkRawConfType(rawConf any) error {
235+
if rawConf == nil {
236+
return nil
237+
}
238+
switch rawConf.(type) {
239+
case int, int32, int64, float32, float64, bool, string, []any, map[string]any, time.Time:
240+
return nil
241+
default:
242+
return fmt.Errorf(
243+
"unsupported type=%T for retrieved config,"+
244+
" ensure that values are wrapped in quotes", rawConf)
245+
}
246+
}

confmap/provider_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func TestNewRetrievedWithOptions(t *testing.T) {
3232
assert.Equal(t, want, ret.Close(context.Background()))
3333
}
3434

35+
func TestNewRetrievedUnsupportedType(t *testing.T) {
36+
_, err := NewRetrieved(errors.New("my error"))
37+
require.Error(t, err)
38+
}
39+
3540
func TestNewRetrievedFromYAML(t *testing.T) {
3641
ret, err := NewRetrievedFromYAML([]byte{})
3742
require.NoError(t, err)

0 commit comments

Comments
 (0)