diff --git a/internal/coreinternal/textutils/encoding.go b/internal/coreinternal/textutils/encoding.go index 209c2cbc7754..512b6a6f7da4 100644 --- a/internal/coreinternal/textutils/encoding.go +++ b/internal/coreinternal/textutils/encoding.go @@ -74,7 +74,7 @@ var encodingOverrides = map[string]encoding.Encoding{ } func lookupEncoding(enc string) (encoding.Encoding, error) { - if e, ok := encodingOverrides[strings.ToLower(enc)]; ok { + if e, ok := EncodingOverridesMap.Get(strings.ToLower(enc)); ok { return e, nil } e, err := ianaindex.IANA.Encoding(enc) @@ -94,3 +94,12 @@ func IsNop(enc string) bool { } return e == encoding.Nop } + +var EncodingOverridesMap = encodingOverridesMap{} + +type encodingOverridesMap struct{} + +func (e *encodingOverridesMap) Get(key string) (encoding.Encoding, bool) { + v, ok := encodingOverrides[key] + return v, ok +} diff --git a/pkg/ottl/ottlfuncs/func_decode.go b/pkg/ottl/ottlfuncs/func_decode.go index 374bb897de79..d6dc5efc0364 100644 --- a/pkg/ottl/ottlfuncs/func_decode.go +++ b/pkg/ottl/ottlfuncs/func_decode.go @@ -12,8 +12,8 @@ import ( "go.opentelemetry.io/collector/pdata/pcommon" "golang.org/x/text/encoding" "golang.org/x/text/encoding/ianaindex" - "golang.org/x/text/encoding/unicode" + "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/textutils" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" ) @@ -87,7 +87,7 @@ func Decode[K any](target ottl.Getter[K], encoding string) (ottl.ExprFunc[K], er } func getEncoding(encoding string) (encoding.Encoding, error) { - if e, ok := encodingOverrides[strings.ToLower(encoding)]; ok { + if e, ok := textutils.EncodingOverridesMap.Get(strings.ToLower(encoding)); ok { return e, nil } e, err := ianaindex.IANA.Encoding(encoding) @@ -101,14 +101,3 @@ func getEncoding(encoding string) (encoding.Encoding, error) { } return e, nil } - -var encodingOverrides = map[string]encoding.Encoding{ - "": unicode.UTF8, - "nop": encoding.Nop, - "ascii": unicode.UTF8, - "us-ascii": unicode.UTF8, - "utf8": unicode.UTF8, - "utf-8": unicode.UTF8, - "utf16": unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM), - "utf-16": unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM), -} diff --git a/pkg/stanza/decode/decode.go b/pkg/stanza/decode/decode.go index af40643a0cc6..0af0e3f3c361 100644 --- a/pkg/stanza/decode/decode.go +++ b/pkg/stanza/decode/decode.go @@ -10,8 +10,9 @@ import ( "golang.org/x/text/encoding" "golang.org/x/text/encoding/ianaindex" - "golang.org/x/text/encoding/unicode" "golang.org/x/text/transform" + + "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/textutils" ) type Decoder struct { @@ -46,20 +47,9 @@ func (d *Decoder) Decode(msgBuf []byte) ([]byte, error) { } } -var encodingOverrides = map[string]encoding.Encoding{ - "": unicode.UTF8, - "nop": encoding.Nop, - "ascii": unicode.UTF8, - "us-ascii": unicode.UTF8, - "utf8": unicode.UTF8, - "utf-8": unicode.UTF8, - "utf16": unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM), - "utf-16": unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM), -} - // LookupEncoding attempts to match the string name provided with a character set encoding. func LookupEncoding(enc string) (encoding.Encoding, error) { - if e, ok := encodingOverrides[strings.ToLower(enc)]; ok { + if e, ok := textutils.EncodingOverridesMap.Get(strings.ToLower(enc)); ok { return e, nil } e, err := ianaindex.IANA.Encoding(enc)