mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-03-30 15:08:28 +00:00

* 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>
37 lines
686 B
Go
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
|
|
}
|