Skip to content

Commit 8c72aed

Browse files
committed
Add strictly typed input feature gate
1 parent 6ca551e commit 8c72aed

File tree

52 files changed

+401
-39
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+401
-39
lines changed

cmd/mdatagen/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ require (
3232
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
3333
github.com/gogo/protobuf v1.3.2 // indirect
3434
github.com/google/uuid v1.6.0 // indirect
35+
github.com/hashicorp/go-version v1.7.0 // indirect
3536
github.com/json-iterator/go v1.1.12 // indirect
3637
github.com/knadh/koanf/maps v0.1.1 // indirect
3738
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
@@ -45,6 +46,7 @@ require (
4546
github.com/prometheus/client_model v0.6.1 // indirect
4647
github.com/prometheus/common v0.54.0 // indirect
4748
github.com/prometheus/procfs v0.15.0 // indirect
49+
go.opentelemetry.io/collector/featuregate v1.9.0 // indirect
4850
go.opentelemetry.io/otel/exporters/prometheus v0.49.0 // indirect
4951
go.opentelemetry.io/otel/sdk v1.27.0 // indirect
5052
go.uber.org/multierr v1.11.0 // indirect

cmd/mdatagen/go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/configauth/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/davecgh/go-spew v1.1.1 // indirect
1515
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
1616
github.com/gogo/protobuf v1.3.2 // indirect
17+
github.com/hashicorp/go-version v1.7.0 // indirect
1718
github.com/knadh/koanf/maps v0.1.1 // indirect
1819
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
1920
github.com/knadh/koanf/v2 v2.1.1 // indirect
@@ -22,6 +23,7 @@ require (
2223
github.com/pmezard/go-difflib v1.0.0 // indirect
2324
go.opentelemetry.io/collector/config/configtelemetry v0.102.1 // indirect
2425
go.opentelemetry.io/collector/confmap v0.102.1 // indirect
26+
go.opentelemetry.io/collector/featuregate v1.9.0 // indirect
2527
go.opentelemetry.io/collector/pdata v1.9.0 // indirect
2628
go.opentelemetry.io/otel v1.27.0 // indirect
2729
go.opentelemetry.io/otel/metric v1.27.0 // indirect

config/configauth/go.sum

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

confmap/confmap.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/knadh/koanf/providers/confmap"
1717
"github.com/knadh/koanf/v2"
1818

19+
"go.opentelemetry.io/collector/confmap/internal"
1920
encoder "go.opentelemetry.io/collector/confmap/internal/mapstructure"
2021
)
2122

@@ -156,7 +157,7 @@ func decodeConfig(m *Conf, result any, errorUnused bool, skipTopLevelUnmarshaler
156157
ErrorUnused: errorUnused,
157158
Result: result,
158159
TagName: "mapstructure",
159-
WeaklyTypedInput: true,
160+
WeaklyTypedInput: !internal.StrictlyTypedInputGate.IsEnabled(),
160161
MatchName: caseSensitiveMatchName,
161162
DecodeHook: mapstructure.ComposeDecodeHookFunc(
162163
expandNilStructPointersHookFunc(),

confmap/converter/expandconverter/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ require (
1212
require (
1313
github.com/davecgh/go-spew v1.1.1 // indirect
1414
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
15+
github.com/hashicorp/go-version v1.7.0 // indirect
1516
github.com/knadh/koanf/maps v0.1.1 // indirect
1617
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
1718
github.com/knadh/koanf/v2 v2.1.1 // indirect
1819
github.com/mitchellh/copystructure v1.2.0 // indirect
1920
github.com/mitchellh/reflectwalk v1.0.2 // indirect
2021
github.com/pmezard/go-difflib v1.0.0 // indirect
22+
go.opentelemetry.io/collector/featuregate v1.9.0 // indirect
2123
go.uber.org/multierr v1.11.0 // indirect
2224
gopkg.in/yaml.v3 v3.0.1 // indirect
2325
)

confmap/converter/expandconverter/go.sum

+10-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

confmap/expand.go

+48-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"regexp"
1212
"strconv"
1313
"strings"
14+
15+
"go.opentelemetry.io/collector/confmap/internal"
1416
)
1517

1618
// schemePattern defines the regexp pattern for scheme names.
@@ -111,22 +113,43 @@ func (mr *Resolver) findAndExpandURI(ctx context.Context, input string) (any, bo
111113
if uri == input {
112114
// If the value is a single URI, then the return value can be anything.
113115
// This is the case `foo: ${file:some_extra_config.yml}`.
114-
return mr.expandURI(ctx, input)
116+
ret, ok, err := mr.expandURI(ctx, input)
117+
if err != nil {
118+
return input, false, err
119+
}
120+
121+
raw, err := ret.AsRaw()
122+
if err != nil {
123+
return input, false, err
124+
}
125+
126+
return raw, ok, nil
115127
}
116128
expanded, changed, err := mr.expandURI(ctx, uri)
117129
if err != nil {
118130
return input, false, err
119131
}
120-
repl, err := toString(expanded)
132+
133+
var repl string
134+
if internal.StrictlyTypedInputGate.IsEnabled() {
135+
repl, err = toStringStrictType(*expanded)
136+
} else {
137+
repl, err = toString(expanded)
138+
}
121139
if err != nil {
122140
return input, false, fmt.Errorf("expanding %v: %w", uri, err)
123141
}
124142
return strings.ReplaceAll(input, uri, repl), changed, err
125143
}
126144

127145
// toString attempts to convert input to a string.
128-
func toString(input any) (string, error) {
146+
func toString(ret *Retrieved) (string, error) {
129147
// This list must be kept in sync with checkRawConfType.
148+
input, err := ret.AsRaw()
149+
if err != nil {
150+
return "", err
151+
}
152+
130153
val := reflect.ValueOf(input)
131154
switch val.Kind() {
132155
case reflect.String:
@@ -142,7 +165,27 @@ func toString(input any) (string, error) {
142165
}
143166
}
144167

145-
func (mr *Resolver) expandURI(ctx context.Context, input string) (any, bool, error) {
168+
func toStringStrictType(ret Retrieved) (string, error) {
169+
input, err := ret.AsRaw()
170+
if err != nil {
171+
return "", err
172+
}
173+
174+
str, ok := ret.getStringRepr()
175+
if !ok {
176+
return "", fmt.Errorf("expected convertable to string value type, got %v(%T)", input, input)
177+
}
178+
179+
val := reflect.ValueOf(input)
180+
switch val.Kind() {
181+
case reflect.String, reflect.Int, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64, reflect.Bool:
182+
return str, nil
183+
default:
184+
return "", fmt.Errorf("expected convertable to string value type, got %q(%T)", input, input)
185+
}
186+
}
187+
188+
func (mr *Resolver) expandURI(ctx context.Context, input string) (*Retrieved, bool, error) {
146189
// strip ${ and }
147190
uri := input[2 : len(input)-1]
148191

@@ -163,8 +206,7 @@ func (mr *Resolver) expandURI(ctx context.Context, input string) (any, bool, err
163206
return nil, false, err
164207
}
165208
mr.closers = append(mr.closers, ret.Close)
166-
val, err := ret.AsRaw()
167-
return val, true, err
209+
return ret, true, nil
168210
}
169211

170212
type location struct {

confmap/go.mod

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/knadh/koanf/providers/confmap v0.1.0
99
github.com/knadh/koanf/v2 v2.1.1
1010
github.com/stretchr/testify v1.9.0
11+
go.opentelemetry.io/collector/featuregate v1.9.0
1112
go.uber.org/goleak v1.3.0
1213
go.uber.org/multierr v1.11.0
1314
go.uber.org/zap v1.27.0
@@ -16,6 +17,7 @@ require (
1617

1718
require (
1819
github.com/davecgh/go-spew v1.1.1 // indirect
20+
github.com/hashicorp/go-version v1.7.0 // indirect
1921
github.com/mitchellh/copystructure v1.2.0 // indirect
2022
github.com/mitchellh/reflectwalk v1.0.2 // indirect
2123
github.com/pmezard/go-difflib v1.0.0 // indirect
@@ -25,3 +27,5 @@ retract (
2527
v0.76.0 // Depends on retracted pdata v1.0.0-rc10 module, use v0.76.1
2628
v0.69.0 // Release failed, use v0.69.1
2729
)
30+
31+
replace go.opentelemetry.io/collector/featuregate => ../featuregate

confmap/go.sum

+8-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

confmap/internal/e2e/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ require (
77
go.opentelemetry.io/collector/confmap v0.102.1
88
go.opentelemetry.io/collector/confmap/provider/envprovider v0.102.1
99
go.opentelemetry.io/collector/confmap/provider/fileprovider v0.102.1
10+
go.opentelemetry.io/collector/featuregate v1.9.0
1011
)
1112

1213
require (
1314
github.com/davecgh/go-spew v1.1.1 // indirect
1415
github.com/go-viper/mapstructure/v2 v2.0.0 // indirect
16+
github.com/hashicorp/go-version v1.7.0 // indirect
1517
github.com/knadh/koanf/maps v0.1.1 // indirect
1618
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
1719
github.com/knadh/koanf/v2 v2.1.1 // indirect

confmap/internal/e2e/go.sum

+12-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)