Check required is added. (#4)

This commit is contained in:
Onur Cinar 2023-06-13 20:39:26 -07:00 committed by GitHub
commit 8cf194fe59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 136 additions and 1 deletions

View file

@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.20
go-version: 1.20.5
- name: Go vet
run: go vet ./...

19
check_required.go Normal file
View file

@ -0,0 +1,19 @@
package checker
import "reflect"
const ResultRequired Result = "REQUIRED"
func checkRequired(value reflect.Value) Result {
if value.IsZero() {
return ResultRequired
}
k := value.Kind()
if (k == reflect.Array || k == reflect.Map || k == reflect.Slice) && value.Len() == 0 {
return ResultRequired
}
return ResultValid
}

104
check_required_test.go Normal file
View file

@ -0,0 +1,104 @@
package checker
import (
"reflect"
"testing"
)
func TestCheckRequiredValidString(t *testing.T) {
s := "valid"
if checkRequired(reflect.ValueOf(s)) != ResultValid {
t.Fail()
}
}
func TestCheckRequiredUninitializedString(t *testing.T) {
var s string
if checkRequired(reflect.ValueOf(s)) != ResultRequired {
t.Fail()
}
}
func TestCheckRequiredEmptyString(t *testing.T) {
s := ""
if checkRequired(reflect.ValueOf(s)) != ResultRequired {
t.Fail()
}
}
func TestCheckRequiredUninitializedNumber(t *testing.T) {
var n int
if checkRequired(reflect.ValueOf(n)) != ResultRequired {
t.Fail()
}
}
func TestCheckRequiredValidSlice(t *testing.T) {
s := []int{1}
if checkRequired(reflect.ValueOf(s)) != ResultValid {
t.Fail()
}
}
func TestCheckRequiredUninitializedSlice(t *testing.T) {
var s []int
if checkRequired(reflect.ValueOf(s)) != ResultRequired {
t.Fail()
}
}
func TestCheckRequiredEmptySlice(t *testing.T) {
s := make([]int, 0)
if checkRequired(reflect.ValueOf(s)) != ResultRequired {
t.Fail()
}
}
func TestCheckRequiredValidArray(t *testing.T) {
s := [1]int{1}
if checkRequired(reflect.ValueOf(s)) != ResultValid {
t.Fail()
}
}
func TestCheckRequiredEmptyArray(t *testing.T) {
s := [1]int{}
if checkRequired(reflect.ValueOf(s)) != ResultRequired {
t.Fail()
}
}
func TestCheckRequiredValidMap(t *testing.T) {
m := map[string]string{
"a": "b",
}
if checkRequired(reflect.ValueOf(m)) != ResultValid {
t.Fail()
}
}
func TestCheckRequiredUninitializedMap(t *testing.T) {
var m map[string]string
if checkRequired(reflect.ValueOf(m)) != ResultRequired {
t.Fail()
}
}
func TestCheckRequiredEmptyMap(t *testing.T) {
m := map[string]string{}
if checkRequired(reflect.ValueOf(m)) != ResultRequired {
t.Fail()
}
}

View file

@ -1,2 +1,14 @@
// Package checker is a Go library for validating user input through struct tags.
package checker
// Result is a unique textual identifier for the mistake.
type Result string
// ResultValid result indicates that the user input is valid.
const ResultValid Result = "VALID"
// Mistake provides the field where the mistake was made and a result for the mistake.
type Mistake struct {
Field string
Result Result
}