Skip to content

Commit

Permalink
Add strictly typed input feature gate
Browse files Browse the repository at this point in the history
  • Loading branch information
mx-psi committed Jun 13, 2024
1 parent 6ca551e commit 8c72aed
Show file tree
Hide file tree
Showing 52 changed files with 401 additions and 39 deletions.
2 changes: 2 additions & 0 deletions cmd/mdatagen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
Expand All @@ -45,6 +46,7 @@ require (
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.54.0 // indirect
github.com/prometheus/procfs v0.15.0 // indirect
go.opentelemetry.io/collector/featuregate v1.9.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.49.0 // indirect
go.opentelemetry.io/otel/sdk v1.27.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions cmd/mdatagen/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions config/configauth/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/knadh/koanf/v2 v2.1.1 // indirect
Expand All @@ -22,6 +23,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.102.1 // indirect
go.opentelemetry.io/collector/confmap v0.102.1 // indirect
go.opentelemetry.io/collector/featuregate v1.9.0 // indirect
go.opentelemetry.io/collector/pdata v1.9.0 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions config/configauth/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion confmap/confmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/v2"

"go.opentelemetry.io/collector/confmap/internal"
encoder "go.opentelemetry.io/collector/confmap/internal/mapstructure"
)

Expand Down Expand Up @@ -156,7 +157,7 @@ func decodeConfig(m *Conf, result any, errorUnused bool, skipTopLevelUnmarshaler
ErrorUnused: errorUnused,
Result: result,
TagName: "mapstructure",
WeaklyTypedInput: true,
WeaklyTypedInput: !internal.StrictlyTypedInputGate.IsEnabled(),
MatchName: caseSensitiveMatchName,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
expandNilStructPointersHookFunc(),
Expand Down
2 changes: 2 additions & 0 deletions confmap/converter/expandconverter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/knadh/koanf/v2 v2.1.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/collector/featuregate v1.9.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
14 changes: 10 additions & 4 deletions confmap/converter/expandconverter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 48 additions & 6 deletions confmap/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"regexp"
"strconv"
"strings"

"go.opentelemetry.io/collector/confmap/internal"
)

// schemePattern defines the regexp pattern for scheme names.
Expand Down Expand Up @@ -111,22 +113,43 @@ func (mr *Resolver) findAndExpandURI(ctx context.Context, input string) (any, bo
if uri == input {
// If the value is a single URI, then the return value can be anything.
// This is the case `foo: ${file:some_extra_config.yml}`.
return mr.expandURI(ctx, input)
ret, ok, err := mr.expandURI(ctx, input)
if err != nil {
return input, false, err
}

raw, err := ret.AsRaw()
if err != nil {
return input, false, err

Check warning on line 123 in confmap/expand.go

View check run for this annotation

Codecov / codecov/patch

confmap/expand.go#L123

Added line #L123 was not covered by tests
}

return raw, ok, nil
}
expanded, changed, err := mr.expandURI(ctx, uri)
if err != nil {
return input, false, err
}
repl, err := toString(expanded)

var repl string
if internal.StrictlyTypedInputGate.IsEnabled() {
repl, err = toStringStrictType(*expanded)

Check warning on line 135 in confmap/expand.go

View check run for this annotation

Codecov / codecov/patch

confmap/expand.go#L135

Added line #L135 was not covered by tests
} else {
repl, err = toString(expanded)
}
if err != nil {
return input, false, fmt.Errorf("expanding %v: %w", uri, err)
}
return strings.ReplaceAll(input, uri, repl), changed, err
}

// toString attempts to convert input to a string.
func toString(input any) (string, error) {
func toString(ret *Retrieved) (string, error) {
// This list must be kept in sync with checkRawConfType.
input, err := ret.AsRaw()
if err != nil {
return "", err

Check warning on line 150 in confmap/expand.go

View check run for this annotation

Codecov / codecov/patch

confmap/expand.go#L150

Added line #L150 was not covered by tests
}

val := reflect.ValueOf(input)
switch val.Kind() {
case reflect.String:
Expand All @@ -142,7 +165,27 @@ func toString(input any) (string, error) {
}
}

func (mr *Resolver) expandURI(ctx context.Context, input string) (any, bool, error) {
func toStringStrictType(ret Retrieved) (string, error) {
input, err := ret.AsRaw()
if err != nil {
return "", err

Check warning on line 171 in confmap/expand.go

View check run for this annotation

Codecov / codecov/patch

confmap/expand.go#L168-L171

Added lines #L168 - L171 were not covered by tests
}

str, ok := ret.getStringRepr()
if !ok {
return "", fmt.Errorf("expected convertable to string value type, got %v(%T)", input, input)

Check warning on line 176 in confmap/expand.go

View check run for this annotation

Codecov / codecov/patch

confmap/expand.go#L174-L176

Added lines #L174 - L176 were not covered by tests
}

val := reflect.ValueOf(input)
switch val.Kind() {
case reflect.String, reflect.Int, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64, reflect.Bool:
return str, nil
default:
return "", fmt.Errorf("expected convertable to string value type, got %q(%T)", input, input)

Check warning on line 184 in confmap/expand.go

View check run for this annotation

Codecov / codecov/patch

confmap/expand.go#L179-L184

Added lines #L179 - L184 were not covered by tests
}
}

func (mr *Resolver) expandURI(ctx context.Context, input string) (*Retrieved, bool, error) {
// strip ${ and }
uri := input[2 : len(input)-1]

Expand All @@ -163,8 +206,7 @@ func (mr *Resolver) expandURI(ctx context.Context, input string) (any, bool, err
return nil, false, err
}
mr.closers = append(mr.closers, ret.Close)
val, err := ret.AsRaw()
return val, true, err
return ret, true, nil
}

type location struct {
Expand Down
4 changes: 4 additions & 0 deletions confmap/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/knadh/koanf/providers/confmap v0.1.0
github.com/knadh/koanf/v2 v2.1.1
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector/featuregate v1.9.0
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
Expand All @@ -16,6 +17,7 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -25,3 +27,5 @@ retract (
v0.76.0 // Depends on retracted pdata v1.0.0-rc10 module, use v0.76.1
v0.69.0 // Release failed, use v0.69.1
)

replace go.opentelemetry.io/collector/featuregate => ../featuregate
12 changes: 8 additions & 4 deletions confmap/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions confmap/internal/e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ require (
go.opentelemetry.io/collector/confmap v0.102.1
go.opentelemetry.io/collector/confmap/provider/envprovider v0.102.1
go.opentelemetry.io/collector/confmap/provider/fileprovider v0.102.1
go.opentelemetry.io/collector/featuregate v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/knadh/koanf/v2 v2.1.1 // indirect
Expand Down
13 changes: 12 additions & 1 deletion confmap/internal/e2e/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8c72aed

Please sign in to comment.