-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add trim left and right normalizers and tests to v2. (#145)
# Describe Request Add trim left and right normalizers and tests to v2. # Change Type New code.
- Loading branch information
Showing
5 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |