41 lines
713 B
Go
41 lines
713 B
Go
package internal
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
func NilChecker(data any) error {
|
|
val := reflect.ValueOf(data)
|
|
|
|
if val.Kind() == reflect.Ptr {
|
|
val = val.Elem()
|
|
}
|
|
|
|
valType := val.Type()
|
|
|
|
if val.Kind() != reflect.Struct {
|
|
return fmt.Errorf("data is not a struct")
|
|
}
|
|
|
|
nilFields := []string{}
|
|
for i := range val.NumField() {
|
|
field := val.Field(i)
|
|
fieldType := valType.Field(i)
|
|
tagValue := fieldType.Tag.Get("nil_checker")
|
|
|
|
if tagValue == "required" {
|
|
if field.Kind() == reflect.Ptr {
|
|
if field.IsNil() {
|
|
nilFields = append(nilFields, fieldType.Name)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if len(nilFields) > 0 {
|
|
return fmt.Errorf("%s is empty", strings.Join(nilFields, ","))
|
|
}
|
|
return nil
|
|
}
|