Check required is added. (#4)
This commit is contained in:
parent
6d168d5ef8
commit
8cf194fe59
4 changed files with 136 additions and 1 deletions
2
.github/workflows/go.yml
vendored
2
.github/workflows/go.yml
vendored
|
|
@ -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
19
check_required.go
Normal 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
104
check_required_test.go
Normal 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()
|
||||
}
|
||||
}
|
||||
12
checker.go
12
checker.go
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue