Skip to content

Commit

Permalink
Rename formatter to transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
minhduc140583 committed Jul 21, 2024
1 parent ad90b0e commit eea8ed2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"github.com/core-go/io/writer"
)

type DelimiterFormatter[T any] struct {
type DelimiterTransformer[T any] struct {
Delimiter string
formatCols map[int]writer.Delimiter
}

func NewDelimiterFormatter[T any](opts ...string) (*DelimiterFormatter[T], error) {
func NewDelimiterTransformer[T any](opts ...string) (*DelimiterTransformer[T], error) {
sep := "|"
if len(opts) > 0 && len(opts[0]) > 0 {
sep = opts[0]
Expand All @@ -27,9 +27,12 @@ func NewDelimiterFormatter[T any](opts ...string) (*DelimiterFormatter[T], error
if err != nil {
return nil, err
}
return &DelimiterFormatter[T]{formatCols: formatCols, Delimiter: sep}, nil
return &DelimiterTransformer[T]{formatCols: formatCols, Delimiter: sep}, nil
}
func NewDelimiterFormatter[T any](opts ...string) (*DelimiterTransformer[T], error) {
return NewDelimiterTransformer[T](opts...)
}

func (f *DelimiterFormatter[T]) Format(ctx context.Context, model *T) string {
func (f *DelimiterTransformer[T]) Transform(ctx context.Context, model *T) string {
return writer.ToTextWithDelimiter(model, f.Delimiter, f.formatCols)
}
25 changes: 0 additions & 25 deletions formatter/fixedlength_formatter.go

This file was deleted.

28 changes: 28 additions & 0 deletions formatter/fixedlength_transformer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package formatter

import (
"context"
"github.com/core-go/io/writer"
"reflect"
)

type FixedLengthTransformer[T any] struct {
formatCols map[int]*writer.FixedLength
}

func NewFixedLengthTransformer[T any]() (*FixedLengthTransformer[T], error) {
var t T
modelType := reflect.TypeOf(t)
formatCols, err := writer.GetIndexes(modelType, "format")
if err != nil {
return nil, err
}
return &FixedLengthTransformer[T]{formatCols: formatCols}, nil
}

func NewFixedLengthFormatter[T any]() (*FixedLengthTransformer[T], error) {
return NewFixedLengthTransformer[T]()
}
func (f *FixedLengthTransformer[T]) Transform(ctx context.Context, model *T) string {
return writer.ToFixedLength(model, f.formatCols)
}

0 comments on commit eea8ed2

Please sign in to comment.