feat: safe websocket server implementation
This commit is contained in:
40
internal/nil_checker.go
Normal file
40
internal/nil_checker.go
Normal file
@@ -0,0 +1,40 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user