Skip to content

Commit

Permalink
Replicate deprecated pkg/stanza/decode.LookupEncoding
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryax committed Feb 28, 2025
1 parent 261808b commit c36bb07
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
3 changes: 1 addition & 2 deletions internal/receiver/scriptedinputsreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"sort"
"time"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/decode"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/split"
Expand Down Expand Up @@ -106,7 +105,7 @@ func (c *Config) Build(set component.TelemetrySettings) (operator.Operator, erro
return nil, err
}

enc, err := decode.LookupEncoding(c.Encoding)
enc, err := lookupEncoding(c.Encoding)
if err != nil {
return nil, err
}
Expand Down
49 changes: 49 additions & 0 deletions internal/receiver/scriptedinputsreceiver/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright Splunk, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package scriptedinputsreceiver

import (
"fmt"
"strings"

"golang.org/x/text/encoding"
"golang.org/x/text/encoding/ianaindex"
"golang.org/x/text/encoding/unicode"
)

var encodingOverrides = map[string]encoding.Encoding{
"utf-16": unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM),
"utf16": unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM),
"utf-8": unicode.UTF8,
"utf8": unicode.UTF8,
"ascii": unicode.UTF8,
"us-ascii": unicode.UTF8,
"nop": encoding.Nop,
"": unicode.UTF8,
}

func lookupEncoding(enc string) (encoding.Encoding, error) {
if e, ok := encodingOverrides[strings.ToLower(enc)]; ok {
return e, nil
}
e, err := ianaindex.IANA.Encoding(enc)
if err != nil {
return nil, fmt.Errorf("unsupported encoding '%s'", enc)
}
if e == nil {
return nil, fmt.Errorf("no charmap defined for encoding '%s'", enc)
}
return e, nil
}

0 comments on commit c36bb07

Please sign in to comment.