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][pkg/stanza] Cleanup csv parser operator files #32109

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
99 changes: 99 additions & 0 deletions pkg/stanza/operator/parser/csv/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package csv // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/csv"

import (
"errors"
"fmt"
"strings"

"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
)

const operatorType = "csv_parser"

func init() {
operator.Register(operatorType, func() operator.Builder { return NewConfig() })
}

// NewConfig creates a new csv parser config with default values
func NewConfig() *Config {
return NewConfigWithID(operatorType)
}

// NewConfigWithID creates a new csv parser config with default values
func NewConfigWithID(operatorID string) *Config {
return &Config{
ParserConfig: helper.NewParserConfig(operatorID, operatorType),
}
}

// Config is the configuration of a csv parser operator.
type Config struct {
helper.ParserConfig `mapstructure:",squash"`

Header string `mapstructure:"header"`
HeaderDelimiter string `mapstructure:"header_delimiter"`
HeaderAttribute string `mapstructure:"header_attribute"`
FieldDelimiter string `mapstructure:"delimiter"`
LazyQuotes bool `mapstructure:"lazy_quotes"`
IgnoreQuotes bool `mapstructure:"ignore_quotes"`
}

// Build will build a csv parser operator.
func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
parserOperator, err := c.ParserConfig.Build(logger)
if err != nil {
return nil, err
}

if c.FieldDelimiter == "" {
c.FieldDelimiter = ","
}

if c.HeaderDelimiter == "" {
c.HeaderDelimiter = c.FieldDelimiter
}

if c.IgnoreQuotes && c.LazyQuotes {
return nil, errors.New("only one of 'ignore_quotes' or 'lazy_quotes' can be true")
}

if len([]rune(c.FieldDelimiter)) != 1 {
return nil, fmt.Errorf("invalid 'delimiter': '%s'", c.FieldDelimiter)
}

if len([]rune(c.HeaderDelimiter)) != 1 {
return nil, fmt.Errorf("invalid 'header_delimiter': '%s'", c.HeaderDelimiter)
}

if c.Header == "" && c.HeaderAttribute == "" {
return nil, errors.New("missing required field 'header' or 'header_attribute'")
}

if c.Header != "" && c.HeaderAttribute != "" {
return nil, errors.New("only one header parameter can be set: 'header' or 'header_attribute'")
}

if c.Header != "" && !strings.Contains(c.Header, c.HeaderDelimiter) {
return nil, errors.New("missing field delimiter in header")
}

p := &Parser{
ParserOperator: parserOperator,
headerAttribute: c.HeaderAttribute,
fieldDelimiter: []rune(c.FieldDelimiter)[0],
headerDelimiter: []rune(c.HeaderDelimiter)[0],
lazyQuotes: c.LazyQuotes,
ignoreQuotes: c.IgnoreQuotes,
}

if c.Header != "" {
p.parse = generateParseFunc(strings.Split(c.Header, c.HeaderDelimiter), p.fieldDelimiter, c.LazyQuotes, c.IgnoreQuotes)
}

return p, nil
}
196 changes: 0 additions & 196 deletions pkg/stanza/operator/parser/csv/csv.go

This file was deleted.

Loading