Skip to content

Commit

Permalink
feat: adding lowercase, trimleft, trimright, trim functions
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Jun 25, 2024
1 parent 3398f1d commit d80acf0
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 8 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@

Transform is a Go library that provides a simple way to transform data from one format to another.

## Installation

```bash
go get github.com/zeiss/go-transform
```

## Usage

```go
type example struct {
Name string `tansform:"trim,lowercase"`
}

t := transform.New()
e := example{Name: " John Doe "}

if err := t.Transform(&e); err != nil {
log.Fatal(err)
}

fmt.Println(e.Name) // Output: john doe
```

## Transformations

* `trim` - Removes leading and trailing whitespace.
* `lowercase` - Converts the string to lowercase.

## License

[MIT](/LICENSE)
[MIT](/LICENSE)
29 changes: 25 additions & 4 deletions transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,22 @@ type FieldLevel interface {
type Func func(fl FieldLevel) error

var internalTransformers = map[string]Func{
"trim": trimFunc,
"trim": trimFunc,
"trim_left": trimLeftFunc,
"trim_right": trimRightFunc,
"lowercase": toLowerCaseFunc,
}

func trimLeftFunc(fl FieldLevel) error {
fl.Field().SetString(strings.TrimLeft(fl.Field().String(), " "))

return nil
}

func trimRightFunc(fl FieldLevel) error {
fl.Field().SetString(strings.TrimRight(fl.Field().String(), " "))

return nil
}

func trimFunc(fl FieldLevel) error {
Expand All @@ -36,6 +51,12 @@ func trimFunc(fl FieldLevel) error {
return nil
}

func toLowerCaseFunc(fl FieldLevel) error {
fl.Field().SetString(strings.ToLower(fl.Field().String()))

return nil
}

var _ FieldLevel = &fieldLevel{}

type fieldLevel struct {
Expand Down Expand Up @@ -100,10 +121,10 @@ func WithTagName(tagName string) TransformerOpt {
}

// Transform ...
func Transform(name string, s interface{}) error {
func Transform(s interface{}) error {
t := NewTransformer()

return t.Transform(name, s)
return t.Transform(s)
}

// NewTransformer ...
Expand All @@ -120,7 +141,7 @@ func NewTransformer(opts ...TransformerOpt) *TransformerImpl {
}

// Transform ...
func (t *TransformerImpl) Transform(name string, s interface{}) error {
func (t *TransformerImpl) Transform(s interface{}) error {
val := reflect.ValueOf(s)
if val.Kind() != reflect.Ptr {
return ErrNoPointer
Expand Down
163 changes: 160 additions & 3 deletions transform_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,177 @@
package transform_test

import (
"fmt"
"log"
"testing"

"github.com/zeiss/go-transform"

"github.com/stretchr/testify/require"
)

func TestStruct(t *testing.T) {
func ExampleTransform() {
type example struct {
Name string `transform:"trim,lowercase"`
}

t := transform.NewTransformer()
e := example{Name: " John Doe "}

if err := t.Transform(&e); err != nil {
log.Fatal(err)
}

fmt.Println(e.Name)
// Output: john doe
}

func BenchmarkStruct(b *testing.B) {
trans := transform.NewTransformer()

type testStruct struct {
Name string `transform:"trim"`
}

for i := 0; i < b.N; i++ {
err := trans.Transform(&testStruct{Name: " test "})
require.NoError(b, err)
}
}

func TestStruct(t *testing.T) {
trans := transform.NewTransformer()

type testStruct struct {
Name string `transform:"trim,lowercase"`
}

tests := []struct {
name string
in *testStruct
out *testStruct
}{
{
name: "nil",
in: nil,
out: nil,
},
{
name: "empty",
in: &testStruct{},
out: &testStruct{},
},
{
name: "string",
in: &testStruct{
Name: " TEST ",
},
out: &testStruct{
Name: "test",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := trans.Transform(tt.in)
require.NoError(t, err)
require.Equal(t, tt.out, tt.in)
})
}
}

func TestStructLowercase(t *testing.T) {
trans := transform.NewTransformer()

type testStruct struct {
Name string `transform:"lowercase"`
}

tests := []struct {
name string
in *testStruct
out *testStruct
}{
{
name: "nil",
in: nil,
out: nil,
},
{
name: "empty",
in: &testStruct{},
out: &testStruct{},
},
{
name: "string",
in: &testStruct{
Name: "TEST",
},
out: &testStruct{
Name: "test",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := trans.Transform(tt.in)
require.NoError(t, err)
require.Equal(t, tt.out, tt.in)
})
}
}

func TestStructTrimRight(t *testing.T) {
trans := transform.NewTransformer()

type testStruct struct {
Name string `transform:"trim_right"`
}

tests := []struct {
name string
in *testStruct
out *testStruct
}{
{
name: "nil",
in: nil,
out: nil,
},
{
name: "empty",
in: &testStruct{},
out: &testStruct{},
},
{
name: "string",
in: &testStruct{
Name: "test ",
},
out: &testStruct{
Name: "test",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := trans.Transform(tt.in)
require.NoError(t, err)
require.Equal(t, tt.out, tt.in)
})
}
}

func TestStructTrimLeft(t *testing.T) {
trans := transform.NewTransformer()

type testStruct struct {
Name string `transform:"trim_left"`
}

tests := []struct {
name string
in *testStruct
Expand All @@ -33,7 +190,7 @@ func TestStruct(t *testing.T) {
{
name: "string",
in: &testStruct{
Name: " test ",
Name: " test",
},
out: &testStruct{
Name: "test",
Expand All @@ -43,7 +200,7 @@ func TestStruct(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := trans.Transform(tt.name, tt.in)
err := trans.Transform(tt.in)
require.NoError(t, err)
require.Equal(t, tt.out, tt.in)
})
Expand Down

0 comments on commit d80acf0

Please sign in to comment.