go-emarshal

[library] easy programmable marshalling of embedded types
git clone https://hhvn.uk/go-emarshal
git clone git://hhvn.uk/go-emarshal
Log | Files | Refs | LICENSE

helpers.go (1004B)


      1 package emarshal
      2 
      3 import (
      4 	"fmt"
      5 	"reflect"
      6 	"strings"
      7 )
      8 
      9 func getMethods(t reflect.Type, prefix string) []reflect.Method {
     10 	var r []reflect.Method
     11 
     12 	for i := 0; i < t.NumMethod(); i++ {
     13 		m := t.Method(i)
     14 		if !strings.HasPrefix(m.Name, prefix) {
     15 			continue
     16 		}
     17 		r = append(r, m)
     18 	}
     19 
     20 	return r
     21 }
     22 
     23 func funcInfo(t reflect.Type) (an int, at []reflect.Type, rn int, rt []reflect.Type) {
     24 	an = t.NumIn()
     25 	at = make([]reflect.Type, an)
     26 	for i := 0; i < an; i++ {
     27 		at[i] = t.In(i)
     28 	}
     29 
     30 	rn = t.NumOut()
     31 	rt = make([]reflect.Type, rn)
     32 	for i := 0; i < rn; i++ {
     33 		rt[i] = t.Out(i)
     34 	}
     35 
     36 	return
     37 }
     38 
     39 func methodInfo(m reflect.Method) (int, []reflect.Type, int, []reflect.Type) {
     40 	an, at, rn, rt := funcInfo(m.Func.Type())
     41 
     42 	return an - 1, at[1:], rn, rt
     43 }
     44 
     45 func methodErr(tn, mn string, err error, desc string) error {
     46 	return fmt.Errorf("%v.%v(): %w: %v", tn, mn, err, desc)
     47 }
     48 
     49 var errorInterface = reflect.TypeOf((*error)(nil)).Elem()
     50 
     51 func isErr(t reflect.Type) bool {
     52 	return t.Implements(errorInterface)
     53 }