Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] Share encoding overrides #35029

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion internal/coreinternal/textutils/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
15 changes: 2 additions & 13 deletions pkg/ottl/ottlfuncs/func_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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),
}
16 changes: 3 additions & 13 deletions pkg/stanza/decode/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down