Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upper and lower normalizers and tests to v2. #146

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions v2/lower.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 (
// nameLower is the name of the lower normalizer.
nameLower = "lower"
)

// Lower maps all Unicode letters in the given value to their lower case.
func Lower(value string) (string, error) {
return strings.ToLower(value), nil
}

// reflectLower maps all Unicode letters in the given value to their lower case.
func reflectLower(value reflect.Value) (reflect.Value, error) {
newValue, err := Lower(value.Interface().(string))
return reflect.ValueOf(newValue), err
}

// makeLower returns the lower normalizer function.
func makeLower(_ string) CheckFunc[reflect.Value] {
return reflectLower
}
47 changes: 47 additions & 0 deletions v2/lower_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 TestLower(t *testing.T) {
input := "CHECKER"
expected := "checker"

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

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

func TestReflectLower(t *testing.T) {
type Person struct {
Name string `checkers:"lower"`
}

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

expected := "checker"

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)
}
}
4 changes: 3 additions & 1 deletion v2/maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ var makers = map[string]MakeCheckFunc{
nameCreditCard: makeCreditCard,
nameDigits: makeDigits,
nameEmail: makeEmail,
nameFQDN: makeFQDN,
nameHTMLEscape: makeHTMLEscape,
nameHTMLUnescape: makeHTMLUnescape,
nameFQDN: makeFQDN,
nameIP: makeIP,
nameIPv4: makeIPv4,
nameIPv6: makeIPv6,
nameISBN: makeISBN,
nameLower: makeLower,
nameLUHN: makeLUHN,
nameMAC: makeMAC,
nameMaxLen: makeMaxLen,
Expand All @@ -37,6 +38,7 @@ var makers = map[string]MakeCheckFunc{
nameTrimLeft: makeTrimLeft,
nameTrimRight: makeTrimRight,
nameTrimSpace: makeTrimSpace,
nameUpper: makeUpper,
nameURL: makeURL,
nameURLEscape: makeURLEscape,
nameURLUnescape: makeURLUnescape,
Expand Down
32 changes: 32 additions & 0 deletions v2/upper.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 (
// nameUpper is the name of the upper normalizer.
nameUpper = "upper"
)

// Upper maps all Unicode letters in the given value to their upper case.
func Upper(value string) (string, error) {
return strings.ToUpper(value), nil
}

// reflectUpper maps all Unicode letters in the given value to their upper case.
func reflectUpper(value reflect.Value) (reflect.Value, error) {
newValue, err := Upper(value.Interface().(string))
return reflect.ValueOf(newValue), err
}

// makeUpper returns the upper normalizer function.
func makeUpper(_ string) CheckFunc[reflect.Value] {
return reflectUpper
}
47 changes: 47 additions & 0 deletions v2/upper_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 TestUpper(t *testing.T) {
input := "checker"
expected := "CHECKER"

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

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

func TestReflectUpper(t *testing.T) {
type Person struct {
Name string `checkers:"upper"`
}

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

expected := "CHECKER"

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