planetmint-go/util/validate_struct.go
Lorenz Herzberger a5cea30e26
92 implement pop result handler (#101)
* ignite scaffold message report-pop-result challenge:Challenge --module dao

* add issuePoPRewards and fix typo

* add util validate struct and report pop test cases

* add TODOs

* replace depricated error

* fix broken import

* add error handler for failed pop issuance

* add placeholder for validator is issuer check

* added changed go.mod and go.sum

* generate docs

* reduce cognitive complexity

---------

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
Co-authored-by: Jürgen Eckel <juergen@riddleandcode.com>
2023-11-16 12:44:56 +01:00

37 lines
686 B
Go

package util
import (
"errors"
"fmt"
"reflect"
)
func ValidateStruct(s interface{}) (err error) {
structType := reflect.TypeOf(s)
kind := structType.Kind()
if kind != reflect.Struct {
return errors.New("input param should be a struct")
}
structVal := reflect.ValueOf(s)
fieldNum := structVal.NumField()
for i := 0; i < fieldNum; i++ {
field := structVal.Field(i)
fieldName := structType.Field(i).Name
isSet := field.IsValid() && !field.IsZero()
// Set to true because bool is always set (i.e. defaults to true)
if field.Type().Kind() == reflect.Bool {
isSet = true
}
if !isSet {
return fmt.Errorf("%s is not set", fieldName)
}
}
return err
}