github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xctx/typed_context.go (about) 1 package xctx 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/go-board/x-go/types" 8 ) 9 10 // TypedContext store typed data in context and can retrieve data with type. 11 type TypedContext struct{ ctx context.Context } 12 13 // NewTyped create derived context from parent context.Context 14 func NewTyped(ctx context.Context) *TypedContext { 15 return &TypedContext{ctx: ctx} 16 } 17 18 func (c *TypedContext) Deadline() (deadline time.Time, ok bool) { 19 return c.ctx.Deadline() 20 } 21 22 func (c *TypedContext) Done() <-chan struct{} { 23 return c.ctx.Done() 24 } 25 26 func (c *TypedContext) Err() error { 27 return c.ctx.Err() 28 } 29 30 func (c *TypedContext) Value(key interface{}) interface{} { 31 return c.ctx.Value(key) 32 } 33 34 func (c *TypedContext) With(v interface{}) { 35 c.ctx = context.WithValue(c.ctx, types.Typeof(v), v) 36 } 37 38 func (c *TypedContext) ReadTyped(v interface{}) (interface{}, bool) { 39 return c.ReadNamedData(types.Typeof(v)) 40 } 41 42 func (c *TypedContext) ReadNamedData(t types.TypeId) (interface{}, bool) { 43 val := c.ctx.Value(t) 44 if val == nil { 45 return nil, false 46 } 47 return val, true 48 } 49 50 func (c *TypedContext) WithInt(i int) { c.With(i) } 51 52 func (c *TypedContext) ReadInt() (int, bool) { 53 val := c.ctx.Value(types.Typeof(int(0))) 54 if val == nil { 55 return 0, false 56 } 57 return val.(int), true 58 } 59 60 func (c *TypedContext) WithInt8(i int8) { c.With(i) } 61 62 func (c *TypedContext) ReadInt8() (int8, bool) { 63 val := c.ctx.Value(types.Typeof(int8(0))) 64 if val == nil { 65 return 0, false 66 } 67 return val.(int8), true 68 } 69 70 func (c *TypedContext) WithInt16(i int16) { c.With(i) } 71 72 func (c *TypedContext) ReadInt16() (int16, bool) { 73 val := c.ctx.Value(types.Typeof(int16(0))) 74 if val == nil { 75 return 0, false 76 } 77 return val.(int16), true 78 } 79 80 func (c *TypedContext) WithInt32(i int32) { c.With(i) } 81 82 func (c *TypedContext) ReadInt32() (int32, bool) { 83 val := c.ctx.Value(types.Typeof(int32(0))) 84 if val == nil { 85 return 0, false 86 } 87 return val.(int32), true 88 } 89 90 func (c *TypedContext) WithInt64(i int64) { c.With(i) } 91 92 func (c *TypedContext) ReadInt64() (int64, bool) { 93 val := c.ctx.Value(types.Typeof(int64(0))) 94 if val == nil { 95 return 0, false 96 } 97 return val.(int64), true 98 } 99 100 func (c *TypedContext) WithUint(i uint) { c.With(i) } 101 102 func (c *TypedContext) ReadUint() (uint, bool) { 103 val := c.ctx.Value(types.Typeof(uint(0))) 104 if val == nil { 105 return 0, false 106 } 107 return val.(uint), true 108 } 109 110 func (c *TypedContext) WithUint8(i uint8) { c.With(i) } 111 112 func (c *TypedContext) ReadUint8() (uint8, bool) { 113 val := c.ctx.Value(types.Typeof(uint8(0))) 114 if val == nil { 115 return 0, false 116 } 117 return val.(uint8), true 118 } 119 120 func (c *TypedContext) WithUint16(i uint16) { c.With(i) } 121 122 func (c *TypedContext) ReadUint16() (uint16, bool) { 123 val := c.ctx.Value(types.Typeof(uint16(0))) 124 if val == nil { 125 return 0, false 126 } 127 return val.(uint16), true 128 } 129 130 func (c *TypedContext) WithUint32(i uint32) { c.With(i) } 131 132 func (c *TypedContext) ReadUint32() (uint32, bool) { 133 val := c.ctx.Value(types.Typeof(uint32(0))) 134 if val == nil { 135 return 0, false 136 } 137 return val.(uint32), true 138 } 139 140 func (c *TypedContext) WithUint64(i uint64) { c.With(i) } 141 142 func (c *TypedContext) ReadUint64() (uint64, bool) { 143 val := c.ctx.Value(types.Typeof(uint64(0))) 144 if val == nil { 145 return 0, false 146 } 147 return val.(uint64), true 148 } 149 150 func (c *TypedContext) WithFloat32(f float32) { c.With(f) } 151 152 func (c *TypedContext) ReadFloat32() (float32, bool) { 153 val := c.ctx.Value(types.Typeof(float32(0))) 154 if val == nil { 155 return 0, false 156 } 157 return val.(float32), true 158 } 159 160 func (c *TypedContext) WithFloat64(f float64) { c.With(f) } 161 162 func (c *TypedContext) ReadFloat64() (float64, bool) { 163 val := c.ctx.Value(types.Typeof(float64(0))) 164 if val == nil { 165 return 0, false 166 } 167 return val.(float64), true 168 } 169 170 func (c *TypedContext) WithBool(b bool) { c.With(b) } 171 172 func (c *TypedContext) ReadBool() (bool, bool) { 173 val := c.ctx.Value(types.Typeof(false)) 174 if val == nil { 175 return false, false 176 } 177 return val.(bool), true 178 } 179 180 func (c *TypedContext) WithString(s string) { c.With(s) } 181 182 func (c *TypedContext) ReadString() (string, bool) { 183 val := c.ctx.Value(types.Typeof("")) 184 if val == nil { 185 return "", false 186 } 187 return val.(string), true 188 } 189 190 func (c *TypedContext) WithTime(t time.Time) { 191 c.ctx = context.WithValue(c.ctx, types.Typeof(t), t) 192 } 193 194 func (c *TypedContext) ReadTime() (time.Time, bool) { 195 val := c.ctx.Value(types.Typeof(time.Time{})) 196 if val == nil { 197 return time.Time{}, false 198 } 199 return val.(time.Time), true 200 }