Skip to content

Commit

Permalink
[chore] Share encoding overrides (#35029)
Browse files Browse the repository at this point in the history
**Description:**
Follow up to
#33942.
I'd like to share the exact override key strings so that they don't get
out of sync. Maybe this is too much effort, but wanted to discuss

It looks like there is some more reuse between textutils and stanza, but
that could be done in a separate PR.
  • Loading branch information
TylerHelmuth authored Sep 5, 2024
1 parent 97533e6 commit 93a6bd9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
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

0 comments on commit 93a6bd9

Please sign in to comment.