Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | package thrift |
| 21 | |
| 22 | import ( |
John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 23 | "context" |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 24 | "encoding/base64" |
| 25 | "fmt" |
| 26 | ) |
| 27 | |
| 28 | const ( |
| 29 | THRIFT_JSON_PROTOCOL_VERSION = 1 |
| 30 | ) |
| 31 | |
| 32 | // for references to _ParseContext see tsimplejson_protocol.go |
| 33 | |
| 34 | // JSON protocol implementation for thrift. |
Jens Geyer | 264a3f3 | 2019-02-23 13:11:40 +0100 | [diff] [blame] | 35 | // Utilizes Simple JSON protocol |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 36 | // |
| 37 | type TJSONProtocol struct { |
| 38 | *TSimpleJSONProtocol |
| 39 | } |
| 40 | |
| 41 | // Constructor |
| 42 | func NewTJSONProtocol(t TTransport) *TJSONProtocol { |
| 43 | v := &TJSONProtocol{TSimpleJSONProtocol: NewTSimpleJSONProtocol(t)} |
Yuxuan 'fishy' Wang | 64c2a4b | 2020-10-10 18:39:32 -0700 | [diff] [blame] | 44 | v.parseContextStack.push(_CONTEXT_IN_TOPLEVEL) |
| 45 | v.dumpContext.push(_CONTEXT_IN_TOPLEVEL) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 46 | return v |
| 47 | } |
| 48 | |
| 49 | // Factory |
| 50 | type TJSONProtocolFactory struct{} |
| 51 | |
| 52 | func (p *TJSONProtocolFactory) GetProtocol(trans TTransport) TProtocol { |
| 53 | return NewTJSONProtocol(trans) |
| 54 | } |
| 55 | |
| 56 | func NewTJSONProtocolFactory() *TJSONProtocolFactory { |
| 57 | return &TJSONProtocolFactory{} |
| 58 | } |
| 59 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 60 | func (p *TJSONProtocol) WriteMessageBegin(ctx context.Context, name string, typeId TMessageType, seqId int32) error { |
Nobuaki Sukegawa | 3aa461b | 2016-04-09 19:46:21 +0900 | [diff] [blame] | 61 | p.resetContextStack() // THRIFT-3735 |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 62 | if e := p.OutputListBegin(); e != nil { |
| 63 | return e |
| 64 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 65 | if e := p.WriteI32(ctx, THRIFT_JSON_PROTOCOL_VERSION); e != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 66 | return e |
| 67 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 68 | if e := p.WriteString(ctx, name); e != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 69 | return e |
| 70 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 71 | if e := p.WriteByte(ctx, int8(typeId)); e != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 72 | return e |
| 73 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 74 | if e := p.WriteI32(ctx, seqId); e != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 75 | return e |
| 76 | } |
| 77 | return nil |
| 78 | } |
| 79 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 80 | func (p *TJSONProtocol) WriteMessageEnd(ctx context.Context) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 81 | return p.OutputListEnd() |
| 82 | } |
| 83 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 84 | func (p *TJSONProtocol) WriteStructBegin(ctx context.Context, name string) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 85 | if e := p.OutputObjectBegin(); e != nil { |
| 86 | return e |
| 87 | } |
| 88 | return nil |
| 89 | } |
| 90 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 91 | func (p *TJSONProtocol) WriteStructEnd(ctx context.Context) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 92 | return p.OutputObjectEnd() |
| 93 | } |
| 94 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 95 | func (p *TJSONProtocol) WriteFieldBegin(ctx context.Context, name string, typeId TType, id int16) error { |
| 96 | if e := p.WriteI16(ctx, id); e != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 97 | return e |
| 98 | } |
| 99 | if e := p.OutputObjectBegin(); e != nil { |
| 100 | return e |
| 101 | } |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 102 | s, e1 := p.TypeIdToString(typeId) |
| 103 | if e1 != nil { |
| 104 | return e1 |
| 105 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 106 | if e := p.WriteString(ctx, s); e != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 107 | return e |
| 108 | } |
| 109 | return nil |
| 110 | } |
| 111 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 112 | func (p *TJSONProtocol) WriteFieldEnd(ctx context.Context) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 113 | return p.OutputObjectEnd() |
| 114 | } |
| 115 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 116 | func (p *TJSONProtocol) WriteFieldStop(ctx context.Context) error { return nil } |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 117 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 118 | func (p *TJSONProtocol) WriteMapBegin(ctx context.Context, keyType TType, valueType TType, size int) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 119 | if e := p.OutputListBegin(); e != nil { |
| 120 | return e |
| 121 | } |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 122 | s, e1 := p.TypeIdToString(keyType) |
| 123 | if e1 != nil { |
| 124 | return e1 |
| 125 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 126 | if e := p.WriteString(ctx, s); e != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 127 | return e |
| 128 | } |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 129 | s, e1 = p.TypeIdToString(valueType) |
| 130 | if e1 != nil { |
| 131 | return e1 |
| 132 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 133 | if e := p.WriteString(ctx, s); e != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 134 | return e |
| 135 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 136 | if e := p.WriteI64(ctx, int64(size)); e != nil { |
Jens Geyer | 0a37870 | 2014-12-10 21:04:28 +0100 | [diff] [blame] | 137 | return e |
| 138 | } |
| 139 | return p.OutputObjectBegin() |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 142 | func (p *TJSONProtocol) WriteMapEnd(ctx context.Context) error { |
Jens Geyer | 0a37870 | 2014-12-10 21:04:28 +0100 | [diff] [blame] | 143 | if e := p.OutputObjectEnd(); e != nil { |
| 144 | return e |
| 145 | } |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 146 | return p.OutputListEnd() |
| 147 | } |
| 148 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 149 | func (p *TJSONProtocol) WriteListBegin(ctx context.Context, elemType TType, size int) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 150 | return p.OutputElemListBegin(elemType, size) |
| 151 | } |
| 152 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 153 | func (p *TJSONProtocol) WriteListEnd(ctx context.Context) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 154 | return p.OutputListEnd() |
| 155 | } |
| 156 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 157 | func (p *TJSONProtocol) WriteSetBegin(ctx context.Context, elemType TType, size int) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 158 | return p.OutputElemListBegin(elemType, size) |
| 159 | } |
| 160 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 161 | func (p *TJSONProtocol) WriteSetEnd(ctx context.Context) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 162 | return p.OutputListEnd() |
| 163 | } |
| 164 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 165 | func (p *TJSONProtocol) WriteBool(ctx context.Context, b bool) error { |
Jens Geyer | bf3a19d | 2013-12-17 21:39:30 +0100 | [diff] [blame] | 166 | if b { |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 167 | return p.WriteI32(ctx, 1) |
Jens Geyer | 2f971e8 | 2014-11-18 21:53:17 +0100 | [diff] [blame] | 168 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 169 | return p.WriteI32(ctx, 0) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 172 | func (p *TJSONProtocol) WriteByte(ctx context.Context, b int8) error { |
| 173 | return p.WriteI32(ctx, int32(b)) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 176 | func (p *TJSONProtocol) WriteI16(ctx context.Context, v int16) error { |
| 177 | return p.WriteI32(ctx, int32(v)) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 180 | func (p *TJSONProtocol) WriteI32(ctx context.Context, v int32) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 181 | return p.OutputI64(int64(v)) |
| 182 | } |
| 183 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 184 | func (p *TJSONProtocol) WriteI64(ctx context.Context, v int64) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 185 | return p.OutputI64(int64(v)) |
| 186 | } |
| 187 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 188 | func (p *TJSONProtocol) WriteDouble(ctx context.Context, v float64) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 189 | return p.OutputF64(v) |
| 190 | } |
| 191 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 192 | func (p *TJSONProtocol) WriteString(ctx context.Context, v string) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 193 | return p.OutputString(v) |
| 194 | } |
| 195 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 196 | func (p *TJSONProtocol) WriteBinary(ctx context.Context, v []byte) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 197 | // JSON library only takes in a string, |
| 198 | // not an arbitrary byte array, to ensure bytes are transmitted |
| 199 | // efficiently we must convert this into a valid JSON string |
| 200 | // therefore we use base64 encoding to avoid excessive escaping/quoting |
| 201 | if e := p.OutputPreValue(); e != nil { |
| 202 | return e |
| 203 | } |
Nobuaki Sukegawa | 3aa461b | 2016-04-09 19:46:21 +0900 | [diff] [blame] | 204 | if _, e := p.write(JSON_QUOTE_BYTES); e != nil { |
Jens Geyer | 2f971e8 | 2014-11-18 21:53:17 +0100 | [diff] [blame] | 205 | return NewTProtocolException(e) |
| 206 | } |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 207 | writer := base64.NewEncoder(base64.StdEncoding, p.writer) |
| 208 | if _, e := writer.Write(v); e != nil { |
Nobuaki Sukegawa | 3aa461b | 2016-04-09 19:46:21 +0900 | [diff] [blame] | 209 | p.writer.Reset(p.trans) // THRIFT-3735 |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 210 | return NewTProtocolException(e) |
| 211 | } |
Jens Geyer | 2f971e8 | 2014-11-18 21:53:17 +0100 | [diff] [blame] | 212 | if e := writer.Close(); e != nil { |
| 213 | return NewTProtocolException(e) |
| 214 | } |
Nobuaki Sukegawa | 3aa461b | 2016-04-09 19:46:21 +0900 | [diff] [blame] | 215 | if _, e := p.write(JSON_QUOTE_BYTES); e != nil { |
Jens Geyer | 2f971e8 | 2014-11-18 21:53:17 +0100 | [diff] [blame] | 216 | return NewTProtocolException(e) |
| 217 | } |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 218 | return p.OutputPostValue() |
| 219 | } |
| 220 | |
| 221 | // Reading methods. |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 222 | func (p *TJSONProtocol) ReadMessageBegin(ctx context.Context) (name string, typeId TMessageType, seqId int32, err error) { |
Nobuaki Sukegawa | 3aa461b | 2016-04-09 19:46:21 +0900 | [diff] [blame] | 223 | p.resetContextStack() // THRIFT-3735 |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 224 | if isNull, err := p.ParseListBegin(); isNull || err != nil { |
| 225 | return name, typeId, seqId, err |
| 226 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 227 | version, err := p.ReadI32(ctx) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 228 | if err != nil { |
| 229 | return name, typeId, seqId, err |
| 230 | } |
| 231 | if version != THRIFT_JSON_PROTOCOL_VERSION { |
| 232 | e := fmt.Errorf("Unknown Protocol version %d, expected version %d", version, THRIFT_JSON_PROTOCOL_VERSION) |
| 233 | return name, typeId, seqId, NewTProtocolExceptionWithType(INVALID_DATA, e) |
| 234 | |
| 235 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 236 | if name, err = p.ReadString(ctx); err != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 237 | return name, typeId, seqId, err |
| 238 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 239 | bTypeId, err := p.ReadByte(ctx) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 240 | typeId = TMessageType(bTypeId) |
| 241 | if err != nil { |
| 242 | return name, typeId, seqId, err |
| 243 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 244 | if seqId, err = p.ReadI32(ctx); err != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 245 | return name, typeId, seqId, err |
| 246 | } |
| 247 | return name, typeId, seqId, nil |
| 248 | } |
| 249 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 250 | func (p *TJSONProtocol) ReadMessageEnd(ctx context.Context) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 251 | err := p.ParseListEnd() |
| 252 | return err |
| 253 | } |
| 254 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 255 | func (p *TJSONProtocol) ReadStructBegin(ctx context.Context) (name string, err error) { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 256 | _, err = p.ParseObjectStart() |
| 257 | return "", err |
| 258 | } |
| 259 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 260 | func (p *TJSONProtocol) ReadStructEnd(ctx context.Context) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 261 | return p.ParseObjectEnd() |
| 262 | } |
| 263 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 264 | func (p *TJSONProtocol) ReadFieldBegin(ctx context.Context) (string, TType, int16, error) { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 265 | b, _ := p.reader.Peek(1) |
| 266 | if len(b) < 1 || b[0] == JSON_RBRACE[0] || b[0] == JSON_RBRACKET[0] { |
| 267 | return "", STOP, -1, nil |
| 268 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 269 | fieldId, err := p.ReadI16(ctx) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 270 | if err != nil { |
| 271 | return "", STOP, fieldId, err |
| 272 | } |
| 273 | if _, err = p.ParseObjectStart(); err != nil { |
| 274 | return "", STOP, fieldId, err |
| 275 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 276 | sType, err := p.ReadString(ctx) |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 277 | if err != nil { |
| 278 | return "", STOP, fieldId, err |
| 279 | } |
| 280 | fType, err := p.StringToTypeId(sType) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 281 | return "", fType, fieldId, err |
| 282 | } |
| 283 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 284 | func (p *TJSONProtocol) ReadFieldEnd(ctx context.Context) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 285 | return p.ParseObjectEnd() |
| 286 | } |
| 287 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 288 | func (p *TJSONProtocol) ReadMapBegin(ctx context.Context) (keyType TType, valueType TType, size int, e error) { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 289 | if isNull, e := p.ParseListBegin(); isNull || e != nil { |
| 290 | return VOID, VOID, 0, e |
| 291 | } |
| 292 | |
| 293 | // read keyType |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 294 | sKeyType, e := p.ReadString(ctx) |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 295 | if e != nil { |
| 296 | return keyType, valueType, size, e |
| 297 | } |
| 298 | keyType, e = p.StringToTypeId(sKeyType) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 299 | if e != nil { |
| 300 | return keyType, valueType, size, e |
| 301 | } |
| 302 | |
| 303 | // read valueType |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 304 | sValueType, e := p.ReadString(ctx) |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 305 | if e != nil { |
| 306 | return keyType, valueType, size, e |
| 307 | } |
| 308 | valueType, e = p.StringToTypeId(sValueType) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 309 | if e != nil { |
| 310 | return keyType, valueType, size, e |
| 311 | } |
| 312 | |
| 313 | // read size |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 314 | iSize, e := p.ReadI64(ctx) |
Jens Geyer | 3bb34df | 2014-12-17 23:06:45 +0100 | [diff] [blame] | 315 | if e != nil { |
| 316 | return keyType, valueType, size, e |
| 317 | } |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 318 | size = int(iSize) |
Jens Geyer | 3bb34df | 2014-12-17 23:06:45 +0100 | [diff] [blame] | 319 | |
| 320 | _, e = p.ParseObjectStart() |
| 321 | return keyType, valueType, size, e |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 324 | func (p *TJSONProtocol) ReadMapEnd(ctx context.Context) error { |
Jens Geyer | 3bb34df | 2014-12-17 23:06:45 +0100 | [diff] [blame] | 325 | e := p.ParseObjectEnd() |
| 326 | if e != nil { |
| 327 | return e |
| 328 | } |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 329 | return p.ParseListEnd() |
| 330 | } |
| 331 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 332 | func (p *TJSONProtocol) ReadListBegin(ctx context.Context) (elemType TType, size int, e error) { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 333 | return p.ParseElemListBegin() |
| 334 | } |
| 335 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 336 | func (p *TJSONProtocol) ReadListEnd(ctx context.Context) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 337 | return p.ParseListEnd() |
| 338 | } |
| 339 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 340 | func (p *TJSONProtocol) ReadSetBegin(ctx context.Context) (elemType TType, size int, e error) { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 341 | return p.ParseElemListBegin() |
| 342 | } |
| 343 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 344 | func (p *TJSONProtocol) ReadSetEnd(ctx context.Context) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 345 | return p.ParseListEnd() |
| 346 | } |
| 347 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 348 | func (p *TJSONProtocol) ReadBool(ctx context.Context) (bool, error) { |
| 349 | value, err := p.ReadI32(ctx) |
Jens Geyer | bf3a19d | 2013-12-17 21:39:30 +0100 | [diff] [blame] | 350 | return (value != 0), err |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 353 | func (p *TJSONProtocol) ReadByte(ctx context.Context) (int8, error) { |
| 354 | v, err := p.ReadI64(ctx) |
Jens Geyer | 5bc8b5a | 2015-09-05 12:50:24 +0200 | [diff] [blame] | 355 | return int8(v), err |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 358 | func (p *TJSONProtocol) ReadI16(ctx context.Context) (int16, error) { |
| 359 | v, err := p.ReadI64(ctx) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 360 | return int16(v), err |
| 361 | } |
| 362 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 363 | func (p *TJSONProtocol) ReadI32(ctx context.Context) (int32, error) { |
| 364 | v, err := p.ReadI64(ctx) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 365 | return int32(v), err |
| 366 | } |
| 367 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 368 | func (p *TJSONProtocol) ReadI64(ctx context.Context) (int64, error) { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 369 | v, _, err := p.ParseI64() |
| 370 | return v, err |
| 371 | } |
| 372 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 373 | func (p *TJSONProtocol) ReadDouble(ctx context.Context) (float64, error) { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 374 | v, _, err := p.ParseF64() |
| 375 | return v, err |
| 376 | } |
| 377 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 378 | func (p *TJSONProtocol) ReadString(ctx context.Context) (string, error) { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 379 | var v string |
| 380 | if err := p.ParsePreValue(); err != nil { |
| 381 | return v, err |
| 382 | } |
Jens Geyer | 2f971e8 | 2014-11-18 21:53:17 +0100 | [diff] [blame] | 383 | f, _ := p.reader.Peek(1) |
| 384 | if len(f) > 0 && f[0] == JSON_QUOTE { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 385 | p.reader.ReadByte() |
| 386 | value, err := p.ParseStringBody() |
| 387 | v = value |
| 388 | if err != nil { |
| 389 | return v, err |
| 390 | } |
Nobuaki Sukegawa | 9b93661 | 2015-10-10 11:28:54 +0900 | [diff] [blame] | 391 | } else if len(f) > 0 && f[0] == JSON_NULL[0] { |
Jens Geyer | 2f971e8 | 2014-11-18 21:53:17 +0100 | [diff] [blame] | 392 | b := make([]byte, len(JSON_NULL)) |
| 393 | _, err := p.reader.Read(b) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 394 | if err != nil { |
| 395 | return v, NewTProtocolException(err) |
| 396 | } |
Jens Geyer | 2f971e8 | 2014-11-18 21:53:17 +0100 | [diff] [blame] | 397 | if string(b) != string(JSON_NULL) { |
| 398 | e := fmt.Errorf("Expected a JSON string, found unquoted data started with %s", string(b)) |
| 399 | return v, NewTProtocolExceptionWithType(INVALID_DATA, e) |
| 400 | } |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 401 | } else { |
Jens Geyer | 2f971e8 | 2014-11-18 21:53:17 +0100 | [diff] [blame] | 402 | e := fmt.Errorf("Expected a JSON string, found unquoted data started with %s", string(f)) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 403 | return v, NewTProtocolExceptionWithType(INVALID_DATA, e) |
| 404 | } |
| 405 | return v, p.ParsePostValue() |
| 406 | } |
| 407 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 408 | func (p *TJSONProtocol) ReadBinary(ctx context.Context) ([]byte, error) { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 409 | var v []byte |
| 410 | if err := p.ParsePreValue(); err != nil { |
| 411 | return nil, err |
| 412 | } |
Jens Geyer | 2f971e8 | 2014-11-18 21:53:17 +0100 | [diff] [blame] | 413 | f, _ := p.reader.Peek(1) |
| 414 | if len(f) > 0 && f[0] == JSON_QUOTE { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 415 | p.reader.ReadByte() |
| 416 | value, err := p.ParseBase64EncodedBody() |
| 417 | v = value |
| 418 | if err != nil { |
| 419 | return v, err |
| 420 | } |
Nobuaki Sukegawa | 9b93661 | 2015-10-10 11:28:54 +0900 | [diff] [blame] | 421 | } else if len(f) > 0 && f[0] == JSON_NULL[0] { |
Jens Geyer | 2f971e8 | 2014-11-18 21:53:17 +0100 | [diff] [blame] | 422 | b := make([]byte, len(JSON_NULL)) |
| 423 | _, err := p.reader.Read(b) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 424 | if err != nil { |
| 425 | return v, NewTProtocolException(err) |
| 426 | } |
Jens Geyer | 2f971e8 | 2014-11-18 21:53:17 +0100 | [diff] [blame] | 427 | if string(b) != string(JSON_NULL) { |
| 428 | e := fmt.Errorf("Expected a JSON string, found unquoted data started with %s", string(b)) |
| 429 | return v, NewTProtocolExceptionWithType(INVALID_DATA, e) |
| 430 | } |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 431 | } else { |
Jens Geyer | 2f971e8 | 2014-11-18 21:53:17 +0100 | [diff] [blame] | 432 | e := fmt.Errorf("Expected a JSON string, found unquoted data started with %s", string(f)) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 433 | return v, NewTProtocolExceptionWithType(INVALID_DATA, e) |
| 434 | } |
Jens Geyer | 2f971e8 | 2014-11-18 21:53:17 +0100 | [diff] [blame] | 435 | |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 436 | return v, p.ParsePostValue() |
| 437 | } |
| 438 | |
John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 439 | func (p *TJSONProtocol) Flush(ctx context.Context) (err error) { |
Jens Geyer | ce8d518 | 2013-12-07 00:08:37 +0100 | [diff] [blame] | 440 | err = p.writer.Flush() |
| 441 | if err == nil { |
John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 442 | err = p.trans.Flush(ctx) |
Jens Geyer | ce8d518 | 2013-12-07 00:08:37 +0100 | [diff] [blame] | 443 | } |
| 444 | return NewTProtocolException(err) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 445 | } |
| 446 | |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 447 | func (p *TJSONProtocol) Skip(ctx context.Context, fieldType TType) (err error) { |
| 448 | return SkipDefaultDepth(ctx, p, fieldType) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | func (p *TJSONProtocol) Transport() TTransport { |
| 452 | return p.trans |
| 453 | } |
| 454 | |
| 455 | func (p *TJSONProtocol) OutputElemListBegin(elemType TType, size int) error { |
| 456 | if e := p.OutputListBegin(); e != nil { |
| 457 | return e |
| 458 | } |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 459 | s, e1 := p.TypeIdToString(elemType) |
| 460 | if e1 != nil { |
| 461 | return e1 |
| 462 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 463 | if e := p.OutputString(s); e != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 464 | return e |
| 465 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 466 | if e := p.OutputI64(int64(size)); e != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 467 | return e |
| 468 | } |
| 469 | return nil |
| 470 | } |
| 471 | |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 472 | func (p *TJSONProtocol) ParseElemListBegin() (elemType TType, size int, e error) { |
| 473 | if isNull, e := p.ParseListBegin(); isNull || e != nil { |
| 474 | return VOID, 0, e |
| 475 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 476 | // We don't really use the ctx in ReadString implementation, |
| 477 | // so this is safe for now. |
| 478 | // We might want to add context to ParseElemListBegin if we start to use |
| 479 | // ctx in ReadString implementation in the future. |
| 480 | sElemType, err := p.ReadString(context.Background()) |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 481 | if err != nil { |
| 482 | return VOID, size, err |
| 483 | } |
| 484 | elemType, err = p.StringToTypeId(sElemType) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 485 | if err != nil { |
| 486 | return elemType, size, err |
| 487 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 488 | nSize, _, err2 := p.ParseI64() |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 489 | size = int(nSize) |
| 490 | return elemType, size, err2 |
| 491 | } |
| 492 | |
| 493 | func (p *TJSONProtocol) readElemListBegin() (elemType TType, size int, e error) { |
| 494 | if isNull, e := p.ParseListBegin(); isNull || e != nil { |
| 495 | return VOID, 0, e |
| 496 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 497 | // We don't really use the ctx in ReadString implementation, |
| 498 | // so this is safe for now. |
| 499 | // We might want to add context to ParseElemListBegin if we start to use |
| 500 | // ctx in ReadString implementation in the future. |
| 501 | sElemType, err := p.ReadString(context.Background()) |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 502 | if err != nil { |
| 503 | return VOID, size, err |
| 504 | } |
| 505 | elemType, err = p.StringToTypeId(sElemType) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 506 | if err != nil { |
| 507 | return elemType, size, err |
| 508 | } |
Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 509 | nSize, _, err2 := p.ParseI64() |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 510 | size = int(nSize) |
| 511 | return elemType, size, err2 |
| 512 | } |
| 513 | |
| 514 | func (p *TJSONProtocol) writeElemListBegin(elemType TType, size int) error { |
| 515 | if e := p.OutputListBegin(); e != nil { |
| 516 | return e |
| 517 | } |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 518 | s, e1 := p.TypeIdToString(elemType) |
| 519 | if e1 != nil { |
| 520 | return e1 |
| 521 | } |
| 522 | if e := p.OutputString(s); e != nil { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 523 | return e |
| 524 | } |
| 525 | if e := p.OutputI64(int64(size)); e != nil { |
| 526 | return e |
| 527 | } |
| 528 | return nil |
| 529 | } |
| 530 | |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 531 | func (p *TJSONProtocol) TypeIdToString(fieldType TType) (string, error) { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 532 | switch byte(fieldType) { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 533 | case BOOL: |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 534 | return "tf", nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 535 | case BYTE: |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 536 | return "i8", nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 537 | case I16: |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 538 | return "i16", nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 539 | case I32: |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 540 | return "i32", nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 541 | case I64: |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 542 | return "i64", nil |
| 543 | case DOUBLE: |
| 544 | return "dbl", nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 545 | case STRING: |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 546 | return "str", nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 547 | case STRUCT: |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 548 | return "rec", nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 549 | case MAP: |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 550 | return "map", nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 551 | case SET: |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 552 | return "set", nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 553 | case LIST: |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 554 | return "lst", nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 555 | } |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 556 | |
| 557 | e := fmt.Errorf("Unknown fieldType: %d", int(fieldType)) |
| 558 | return "", NewTProtocolExceptionWithType(INVALID_DATA, e) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 561 | func (p *TJSONProtocol) StringToTypeId(fieldType string) (TType, error) { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 562 | switch fieldType { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 563 | case "tf": |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 564 | return TType(BOOL), nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 565 | case "i8": |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 566 | return TType(BYTE), nil |
| 567 | case "i16": |
| 568 | return TType(I16), nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 569 | case "i32": |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 570 | return TType(I32), nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 571 | case "i64": |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 572 | return TType(I64), nil |
| 573 | case "dbl": |
| 574 | return TType(DOUBLE), nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 575 | case "str": |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 576 | return TType(STRING), nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 577 | case "rec": |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 578 | return TType(STRUCT), nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 579 | case "map": |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 580 | return TType(MAP), nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 581 | case "set": |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 582 | return TType(SET), nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 583 | case "lst": |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 584 | return TType(LIST), nil |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 585 | } |
Jens Geyer | 8a0f8d1 | 2013-09-10 21:30:41 +0200 | [diff] [blame] | 586 | |
| 587 | e := fmt.Errorf("Unknown type identifier: %s", fieldType) |
| 588 | return TType(STOP), NewTProtocolExceptionWithType(INVALID_DATA, e) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 589 | } |
Yuxuan 'fishy' Wang | c4d1c0d | 2020-12-16 17:10:48 -0800 | [diff] [blame^] | 590 | |
| 591 | var _ TConfigurationSetter = (*TJSONProtocol)(nil) |