Add trim left and right normalizers and tests to v2.

This commit is contained in:
Onur Cinar 2024-12-27 14:53:08 +00:00 committed by GitHub
commit 989f7d05ef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 160 additions and 0 deletions

View file

@ -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,

32
v2/trim_left.go Normal file
View file

@ -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
v2/trim_left_test.go Normal file
View file

@ -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
v2/trim_right.go Normal file
View file

@ -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
v2/trim_right_test.go Normal file
View file

@ -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)
}
}