Skip to content

Commit

Permalink
[chore][pkg/stanza] Cleanup csv parser operator files (#32109)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski authored Apr 2, 2024
1 parent 8e67faa commit adb1aa0
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 196 deletions.
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

0 comments on commit adb1aa0

Please sign in to comment.