func DecInterface(dec *msgpack.Decoder) (interface{}, error) { var st status err := dec.Decode(&st) if err != nil { return nil, e.Forward(err) } switch st { case statusNotNil: var typename string err = dec.Decode(&typename) if err != nil { return nil, e.Forward(err) } t, err := types.GetType(typename) if err != nil { return nil, e.Forward(err) } ptr := types.Make(reflect.PtrTo(t)) err = dec.DecodeValue(ptr) if err != nil { return nil, e.Forward(err) } return reflect.Indirect(ptr).Interface(), nil case statusNil: return nil, nil } panic("not here") }
func (t *Time) DecodeMsgpack(dec *msgpack.Decoder) error { var status statusValue err := dec.Decode(&status) if err != nil { return err } switch status { case stNotIsValid: t.t = nil case stIsValid: var typename string err := dec.Decode(&typename) if err != nil { return err } typ, err := types.GetType(typename) if err != nil { return e.Forward(err) } ptr := types.Make(reflect.PtrTo(typ)) err = dec.DecodeValue(ptr) if err != nil { return err } t.t = ptr.Elem().Interface() default: return e.New("invalid data") } return nil }
func (c *Constructors) Construct(name string, args ...interface{}) (reflect.Value, error) { c.lck.Lock() defer c.lck.Unlock() con, found := c.constructors[name] if !found { t, err := types.GetType(name) if err != nil { return reflect.Value{}, e.New(ErrTypeNotFound) } return types.Make(t), nil } vals := make([]reflect.Value, 0, len(args)) for _, arg := range args { vals = append(vals, reflect.ValueOf(arg)) } retvals := con.Call(vals) if len(retvals) != 2 { return reflect.Value{}, e.New(ErrInvReturn) } if retvals[1].Interface() != nil { err, ok := retvals[1].Interface().(error) if !ok { return reflect.Value{}, e.New(ErrInvReturn) } if err != nil { return reflect.Value{}, e.Forward(err) } } return retvals[0], nil }
func (r *respCall) DecodeMsgpack(dec *msgpack.Decoder) error { var proto util.Msg err := dec.Decode(&proto) if err != nil { return e.Forward(err) } switch proto { case util.ErrNil: case util.ErrOk: err := dec.Decode(&proto) if err != nil { return e.Forward(err) } switch proto { case util.ErrorPriv: var errtype string err := dec.Decode(&errtype) if err != nil { return e.Forward(err) } t, err := types.GetType(errtype) if err != nil { return e.Forward(err) } ptr := types.Make(reflect.PtrTo(t)) err = dec.DecodeValue(ptr) if err != nil { return e.Forward(err) } r.Err = reflect.Indirect(ptr).Interface().(error) case util.ErrorInt: var errStr string err := dec.Decode(&errStr) if err != nil { return e.Forward(err) } r.Err = errors.New(errStr) } default: return e.New("protocol error") } var lenght int err = dec.Decode(&lenght) if err != nil { return e.Forward(err) } if lenght > 0 { r.Vals = make([]reflect.Value, lenght) for i := range r.Vals { r.Vals[i], err = util.MsgPackDecode(dec) if err != nil { return e.Forward(err) } } } return nil }
func (g *Gob) Decode(b []byte) (interface{}, error) { buf := bytes.NewBuffer(b) dec := gob.NewDecoder(buf) val := types.Make(types.Type(g.TypeName)) err := dec.DecodeValue(val) if err != nil { return nil, e.New(err) } return val.Interface(), nil }
func (t *txMongoDb) Get(key string) (interface{}, error) { val := types.Make(t.tentry) inter := val.Interface() err := t.c.Find(bson.M{"key": key}).One(inter) if _, ok := err.(*mgo.QueryError); ok { return nil, e.New(ErrKeyNotFound) } else if e.Contains(err, "not found") { return nil, e.New(ErrKeyNotFound) } else if err != nil { return nil, e.New(err) } return inter, nil }
func decImg(b []byte) (Image, error) { buf := bytes.NewBuffer(b) dec := gob.NewDecoder(buf) var typename string err := dec.Decode(&typename) if err != nil { return nil, e.New(err) } val := types.Make(types.Type(typename)) err = dec.DecodeValue(val) if err != nil { return nil, e.New(err) } return val.Interface().(Image), nil }
func (c *cursorMongoDb) Seek(wanted string) (key string, data interface{}) { c.iter = c.c.Find(bson.M{"key": bson.M{"$gte": wanted}}).Sort("key").Iter() c.dir = LeftToRight val := types.Make(c.tentry) inter := val.Interface() c.iter.Next(inter) key = getKey(inter) if key == "" { return "", nil } return key, inter }
func (c *cursorMongoDb) Last() (key string, data interface{}) { c.iter = c.c.Find(nil).Sort("-key").Iter() c.dir = RightToLeft val := types.Make(c.tentry) inter := val.Interface() c.iter.Next(inter) key = getKey(inter) if key == "" { return "", nil } return key, inter }
func (c *cursorMongoDb) Prev() (key string, data interface{}) { if c.iter == nil { return "", nil } if c.dir != RightToLeft { return "", nil } val := types.Make(c.tentry) inter := val.Interface() c.iter.Next(inter) key = getKey(inter) if key == "" { return "", nil } return key, inter }
func (r *RespInst) DecodeMsgpack(dec *msgpack.Decoder) error { err := dec.Decode(&r.Inst) if err != nil { return e.Forward(err) } var proto util.Msg err = dec.Decode(&proto) if err != nil { return e.Forward(err) } switch proto { case util.ErrNil: case util.ErrOk: err := dec.Decode(&proto) if err != nil { return e.Forward(err) } switch proto { case util.ErrorPriv: var errtype string err := dec.Decode(&errtype) if err != nil { return e.Forward(err) } t, err := types.GetType(errtype) if err != nil { return e.Forward(err) } ptr := types.Make(reflect.PtrTo(t)) err = dec.DecodeValue(ptr) if err != nil { return e.Forward(err) } r.Err = reflect.Indirect(ptr).Interface().(error) case util.ErrorInt: var errStr string err := dec.Decode(&errStr) if err != nil { return e.Forward(err) } r.Err = errors.New(errStr) } default: return e.New("protocol error") } return nil }
// MsgPackDecode decode from dec a value represented by a reflect.Value type. func MsgPackDecode(dec *msgpack.Decoder) (reflect.Value, error) { var proto Msg err := dec.Decode(&proto) if err != nil { return reflect.Value{}, e.Forward(err) } switch proto { case NotValid: return reflect.Value{}, nil case Func: return reflect.Value{}, nil case Valid: default: return reflect.Value{}, e.New("protocol error") } err = dec.Decode(&proto) if err != nil { return reflect.Value{}, e.Forward(err) } switch proto { case ArgNil: err = dec.Decode(&proto) if err != nil { return reflect.Value{}, e.Forward(err) } switch proto { case ZeroArg: return reflect.Value{}, nil case NoZeroArg: var typename string err = dec.Decode(&typename) if err != nil { return reflect.Value{}, e.Forward(err) } // Work arround for this two special types. switch typename { case "error": return reflect.Value{}, nil case "interface {}": return reflect.Value{}, nil } t, err := types.GetType(typename) if err != nil { return reflect.Value{}, e.Forward(err) } val := reflect.New(t) ind := reflect.Indirect(val) return ind, nil default: return reflect.Value{}, e.New("protocol error") } case NotNil: default: return reflect.Value{}, e.New("protocol error") } err = dec.Decode(&proto) if err != nil { return reflect.Value{}, e.Forward(err) } switch proto { case ZeroArg: var typename string err = dec.Decode(&typename) if err != nil { return reflect.Value{}, e.Forward(err) } t, err := types.GetType(typename) if err != nil { return reflect.Value{}, e.Forward(err) } return reflect.Zero(t), nil case NoZeroArg: default: return reflect.Value{}, e.New("protocol error") } err = dec.Decode(&proto) if err != nil { return reflect.Value{}, e.Forward(err) } switch proto { case ArgError: var errStr string err = dec.Decode(&errStr) if err != nil { return reflect.Value{}, e.Forward(err) } return reflect.ValueOf(errors.New(errStr)), nil case ArgNormal: var typename string err = dec.Decode(&typename) if err != nil { return reflect.Value{}, e.Forward(err) } t, err := types.GetType(typename) if err != nil { return reflect.Value{}, e.Forward(err) } ptr := types.Make(reflect.PtrTo(t)) err = dec.DecodeValue(ptr) if err != nil { return reflect.Value{}, e.Forward(err) } val := reflect.Indirect(ptr) return val, nil default: return reflect.Value{}, e.New("protocol error") } }
func (a *Alive) Connect() error { if a.events == nil { a.events = make(chan event.Event) } a.closePing = make(chan bool) resolved, err := dns.ResolveUrl(a.Url) if e.Contains(err, "invalid domain name") { resolved = a.Url } else if err != nil { a.setConnected(false) return e.Forward(err) } for i := 0; i < a.Tries; i++ { a.conn, err = net.Dial(resolved.Scheme, resolved.Host) if e.Contains(err, definitions.NetErrorConnectionRefused) || e.Contains(err, definitions.NetErrorTimeOut) || e.Contains(err, definitions.ErrCheckIsAliveNoRoute) || e.Contains(err, definitions.ErrCheckIsAliveNoAnswerFromResolver) { time.Sleep(a.Wait) continue } else if err != nil { a.setConnected(false) return e.Forward(err) } break } if a.conn == nil { a.setConnected(false) if err != nil { return e.Push(e.New(err), definitions.ErrCheckIsAliveCantConnect) } return e.New(definitions.ErrCheckIsAliveCantConnect) } a.setConnected(true) dec := gob.NewDecoder(a.conn) enc := gob.NewEncoder(a.conn) go func() { defer func() { if a.conn != nil { a.conn.Close() } a.setConnected(false) }() for { select { case <-time.After(a.Wait): err := a.conn.SetWriteDeadline(time.Now().Add(5 * time.Second)) if err != nil { return } err = enc.Encode("ping") if err != nil { a.Statuses.Log(status.Verbose, "Alive: Ping encode error: %v", err) return } case <-a.closePing: return } } }() go func() { defer func() { if a.conn != nil { a.conn.Close() } a.setConnected(false) }() for { var typ string err := dec.Decode(&typ) if err != nil { a.Statuses.Log(status.Verbose, "Alive: Decode error: %v", err) return } t, err := types.GetType(typ) if err != nil { a.Statuses.Log(status.Verbose, "Alive: GetType error: %v", err) return } ptr := types.Make(reflect.PtrTo(t)) err = dec.DecodeValue(ptr) if err != nil { a.Statuses.Log(status.Verbose, "Alive: DecodeValue error: %v", err) return } val := reflect.Indirect(ptr) if val.IsValid() { a.events <- val.Interface().(event.Event) } } }() return nil }