github.com/pubgo/xprocess@v0.1.11/xprocess_future/future.value.go (about) 1 package xprocess_future 2 3 import ( 4 "reflect" 5 "sync" 6 7 "github.com/pubgo/xerror" 8 "github.com/pubgo/xerror/xerror_abc" 9 "github.com/pubgo/xprocess/xprocess_abc" 10 "github.com/pubgo/xprocess/xutil" 11 ) 12 13 var _ xprocess_abc.FutureValue = (*futureValue)(nil) 14 15 func newFutureValue() *futureValue { return &futureValue{} } 16 17 type futureValue struct { 18 done sync.Once 19 values []reflect.Value 20 err error 21 valFn func() []reflect.Value 22 } 23 24 func (v *futureValue) Assert(format string, a ...interface{}) { xerror.PanicF(v.Err(), format, a...) } 25 func (v *futureValue) Err() error { _ = v.getVal(); return v.err } 26 func (v *futureValue) setErr(err error) *futureValue { v.err = err; return v } 27 func (v *futureValue) Raw() []reflect.Value { return v.getVal() } 28 func (v *futureValue) String() string { return valueStr(v.getVal()...) } 29 30 func (v *futureValue) Get() interface{} { 31 val := v.getVal() 32 if len(val) == 0 || !val[0].IsValid() { 33 return nil 34 } 35 36 return val[0].Interface() 37 } 38 39 func (v *futureValue) getVal() []reflect.Value { 40 v.done.Do(func() { 41 if v.valFn != nil { 42 v.values = v.valFn() 43 } 44 }) 45 return v.values 46 } 47 48 func (v *futureValue) Value(fn interface{}) (gErr error) { 49 defer xerror.Resp(func(err xerror_abc.XErr) { 50 gErr = err.WrapF("input:%s, func:%s", valueStr(v.getVal()...), reflect.TypeOf(fn)) 51 }) 52 53 xerror.Assert(fn == nil, "[fn] should not be nil") 54 xerror.Panic(v.Err()) 55 56 xutil.FuncValue(fn)(v.getVal()...) 57 return 58 } 59 60 var _ xprocess_abc.Value = (*value)(nil) 61 62 type value struct { 63 err error 64 val interface{} 65 } 66 67 func (v *value) Assert(format string, a ...interface{}) { xerror.PanicF(v.Err(), format, a...) } 68 func (v *value) Err() error { return v.err } 69 func (v *value) Value() interface{} { return v.val }