Skip to content

Commit

Permalink
Add upper and lower normalizers and tests to v2. (#146)
Browse files Browse the repository at this point in the history
# Describe Request

Add upper and lower normalizers and tests to v2.

# Change Type

New code.
  • Loading branch information
cinar authored Dec 27, 2024
1 parent e24eb02 commit 36a112c
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 1 deletion.
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)
}
}

0 comments on commit 36a112c

Please sign in to comment.