Skip to content

Commit

Permalink
Add trim left and right normalizers and tests to v2. (#145)
Browse files Browse the repository at this point in the history
# Describe Request

Add trim left and right normalizers and tests to v2.

# Change Type

New code.
  • Loading branch information
cinar authored Dec 27, 2024
1 parent 1cff930 commit e24eb02
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
2 changes: 2 additions & 0 deletions v2/maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var makers = map[string]MakeCheckFunc{
nameMaxLen: makeMaxLen,
nameMinLen: makeMinLen,
nameRequired: makeRequired,
nameTrimLeft: makeTrimLeft,
nameTrimRight: makeTrimRight,
nameTrimSpace: makeTrimSpace,
nameURL: makeURL,
nameURLEscape: makeURLEscape,
Expand Down
32 changes: 32 additions & 0 deletions v2/trim_left.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2023-2024 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker

package v2

import (
"reflect"
"strings"
)

const (
// nameTrimLeft is the name of the trim left normalizer.
nameTrimLeft = "trim-left"
)

// TrimLeft returns the value of the string with whitespace removed from the beginning.
func TrimLeft(value string) (string, error) {
return strings.TrimLeft(value, " \t"), nil
}

// reflectTrimLeft returns the value of the string with whitespace removed from the beginning.
func reflectTrimLeft(value reflect.Value) (reflect.Value, error) {
newValue, err := TrimLeft(value.Interface().(string))
return reflect.ValueOf(newValue), err
}

// makeTrimLeft returns the trim left normalizer function.
func makeTrimLeft(_ string) CheckFunc[reflect.Value] {
return reflectTrimLeft
}
47 changes: 47 additions & 0 deletions v2/trim_left_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2023-2024 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker

package v2_test

import (
"testing"

v2 "github.com/cinar/checker/v2"
)

func TestTrimLeft(t *testing.T) {
input := " test "
expected := "test "

actual, err := v2.TrimLeft(input)
if err != nil {
t.Fatal(err)
}

if actual != expected {
t.Fatalf("actual %s expected %s", actual, expected)
}
}

func TestReflectTrimLeft(t *testing.T) {
type Person struct {
Name string `checkers:"trim-left"`
}

person := &Person{
Name: " test ",
}

expected := "test "

errs, ok := v2.CheckStruct(person)
if !ok {
t.Fatalf("got unexpected errors %v", errs)
}

if person.Name != expected {
t.Fatalf("actual %s expected %s", person.Name, expected)
}
}
32 changes: 32 additions & 0 deletions v2/trim_right.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2023-2024 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker

package v2

import (
"reflect"
"strings"
)

const (
// nameTrimRight is the name of the trim right normalizer.
nameTrimRight = "trim-right"
)

// TrimRight returns the value of the string with whitespace removed from the end.
func TrimRight(value string) (string, error) {
return strings.TrimRight(value, " \t"), nil
}

// reflectTrimRight returns the value of the string with whitespace removed from the end.
func reflectTrimRight(value reflect.Value) (reflect.Value, error) {
newValue, err := TrimRight(value.Interface().(string))
return reflect.ValueOf(newValue), err
}

// makeTrimRight returns the trim right normalizer function.
func makeTrimRight(_ string) CheckFunc[reflect.Value] {
return reflectTrimRight
}
47 changes: 47 additions & 0 deletions v2/trim_right_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2023-2024 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker

package v2_test

import (
"testing"

v2 "github.com/cinar/checker/v2"
)

func TestTrimRight(t *testing.T) {
input := " test "
expected := " test"

actual, err := v2.TrimRight(input)
if err != nil {
t.Fatal(err)
}

if actual != expected {
t.Fatalf("actual %s expected %s", actual, expected)
}
}

func TestReflectTrimRight(t *testing.T) {
type Person struct {
Name string `checkers:"trim-right"`
}

person := &Person{
Name: " test ",
}

expected := " test"

errs, ok := v2.CheckStruct(person)
if !ok {
t.Fatalf("got unexpected errors %v", errs)
}

if person.Name != expected {
t.Fatalf("actual %s expected %s", person.Name, expected)
}
}

0 comments on commit e24eb02

Please sign in to comment.