Christian Lavoie | afc6d8f | 2011-02-20 02:39:19 +0000 | [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 | |
| 23 | const ( |
| 24 | VERSION_MASK = 0xffff0000 |
| 25 | VERSION_1 = 0x80010000 |
| 26 | ) |
| 27 | |
| 28 | type EmptyInterface interface{} |
| 29 | |
| 30 | type TProtocol interface { |
| 31 | WriteMessageBegin(name string, typeId TMessageType, seqid int32) TProtocolException |
| 32 | WriteMessageEnd() TProtocolException |
| 33 | WriteStructBegin(name string) TProtocolException |
| 34 | WriteStructEnd() TProtocolException |
| 35 | WriteFieldBegin(name string, typeId TType, id int16) TProtocolException |
| 36 | WriteFieldEnd() TProtocolException |
| 37 | WriteFieldStop() TProtocolException |
| 38 | WriteMapBegin(keyType TType, valueType TType, size int) TProtocolException |
| 39 | WriteMapEnd() TProtocolException |
| 40 | WriteListBegin(elemType TType, size int) TProtocolException |
| 41 | WriteListEnd() TProtocolException |
| 42 | WriteSetBegin(elemType TType, size int) TProtocolException |
| 43 | WriteSetEnd() TProtocolException |
| 44 | WriteBool(value bool) TProtocolException |
| 45 | WriteByte(value byte) TProtocolException |
| 46 | WriteI16(value int16) TProtocolException |
| 47 | WriteI32(value int32) TProtocolException |
| 48 | WriteI64(value int64) TProtocolException |
| 49 | WriteDouble(value float64) TProtocolException |
| 50 | WriteString(value string) TProtocolException |
| 51 | WriteBinary(value []byte) TProtocolException |
| 52 | |
| 53 | ReadMessageBegin() (name string, typeId TMessageType, seqid int32, err TProtocolException) |
| 54 | ReadMessageEnd() TProtocolException |
| 55 | ReadStructBegin() (name string, err TProtocolException) |
| 56 | ReadStructEnd() TProtocolException |
| 57 | ReadFieldBegin() (name string, typeId TType, id int16, err TProtocolException) |
| 58 | ReadFieldEnd() TProtocolException |
| 59 | ReadMapBegin() (keyType TType, valueType TType, size int, err TProtocolException) |
| 60 | ReadMapEnd() TProtocolException |
| 61 | ReadListBegin() (elemType TType, size int, err TProtocolException) |
| 62 | ReadListEnd() TProtocolException |
| 63 | ReadSetBegin() (elemType TType, size int, err TProtocolException) |
| 64 | ReadSetEnd() TProtocolException |
| 65 | ReadBool() (value bool, err TProtocolException) |
| 66 | ReadByte() (value byte, err TProtocolException) |
| 67 | ReadI16() (value int16, err TProtocolException) |
| 68 | ReadI32() (value int32, err TProtocolException) |
| 69 | ReadI64() (value int64, err TProtocolException) |
| 70 | ReadDouble() (value float64, err TProtocolException) |
| 71 | ReadString() (value string, err TProtocolException) |
| 72 | ReadBinary() (value []byte, err TProtocolException) |
| 73 | |
| 74 | Skip(fieldType TType) (err TProtocolException) |
| 75 | Flush() (err TProtocolException) |
| 76 | |
| 77 | Transport() TTransport |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * The maximum recursive depth the skip() function will traverse before |
| 82 | * throwing a TException. |
| 83 | */ |
| 84 | var ( |
| 85 | MaxSkipDepth = 1<<31 - 1 |
| 86 | ) |
| 87 | |
| 88 | /** |
| 89 | * Specifies the maximum recursive depth that the skip function will |
| 90 | * traverse before throwing a TException. This is a global setting, so |
| 91 | * any call to skip in this JVM will enforce this value. |
| 92 | * |
| 93 | * @param depth the maximum recursive depth. A value of 2 would allow |
| 94 | * the skip function to skip a structure or collection with basic children, |
| 95 | * but it would not permit skipping a struct that had a field containing |
| 96 | * a child struct. A value of 1 would only allow skipping of simple |
| 97 | * types and empty structs/collections. |
| 98 | */ |
| 99 | func SetMaxSkipDepth(depth int) { |
| 100 | MaxSkipDepth = depth |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Skips over the next data element from the provided input TProtocol object. |
| 105 | * |
| 106 | * @param prot the protocol object to read from |
| 107 | * @param type the next value will be intepreted as this TType value. |
| 108 | */ |
| 109 | func SkipDefaultDepth(prot TProtocol, typeId TType) (err TProtocolException) { |
| 110 | return Skip(prot, typeId, MaxSkipDepth) |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Skips over the next data element from the provided input TProtocol object. |
| 115 | * |
| 116 | * @param prot the protocol object to read from |
| 117 | * @param type the next value will be intepreted as this TType value. |
| 118 | * @param maxDepth this function will only skip complex objects to this |
| 119 | * recursive depth, to prevent Java stack overflow. |
| 120 | */ |
| 121 | func Skip(self TProtocol, fieldType TType, maxDepth int) (err TProtocolException) { |
| 122 | switch fieldType { |
| 123 | case STOP: |
| 124 | return |
| 125 | case BOOL: |
| 126 | _, err = self.ReadBool() |
| 127 | return |
| 128 | case BYTE: |
| 129 | _, err = self.ReadByte() |
| 130 | return |
| 131 | case I16: |
| 132 | _, err = self.ReadI16() |
| 133 | return |
| 134 | case I32: |
| 135 | _, err = self.ReadI32() |
| 136 | return |
| 137 | case I64: |
| 138 | _, err = self.ReadI64() |
| 139 | return |
| 140 | case DOUBLE: |
| 141 | _, err = self.ReadDouble() |
| 142 | return |
| 143 | case STRING: |
| 144 | _, err = self.ReadString() |
| 145 | return |
| 146 | case STRUCT: |
| 147 | { |
| 148 | _, err = self.ReadStructBegin() |
| 149 | if err != nil { |
| 150 | return |
| 151 | } |
| 152 | for { |
| 153 | _, typeId, _, _ := self.ReadFieldBegin() |
| 154 | if typeId == STOP { |
| 155 | break |
| 156 | } |
| 157 | Skip(self, typeId, maxDepth-1) |
| 158 | self.ReadFieldEnd() |
| 159 | } |
| 160 | return self.ReadStructEnd() |
| 161 | } |
| 162 | case MAP: |
| 163 | { |
| 164 | keyType, valueType, l, err := self.ReadMapBegin() |
| 165 | if err != nil { |
| 166 | return err |
| 167 | } |
| 168 | size := int(l) |
| 169 | for i := 0; i < size; i++ { |
| 170 | Skip(self, keyType, maxDepth-1) |
| 171 | self.Skip(valueType) |
| 172 | } |
| 173 | return self.ReadMapEnd() |
| 174 | } |
| 175 | case SET: |
| 176 | { |
| 177 | elemType, l, err := self.ReadSetBegin() |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | size := int(l) |
| 182 | for i := 0; i < size; i++ { |
| 183 | Skip(self, elemType, maxDepth-1) |
| 184 | } |
| 185 | return self.ReadSetEnd() |
| 186 | } |
| 187 | case LIST: |
| 188 | { |
| 189 | elemType, l, err := self.ReadListBegin() |
| 190 | if err != nil { |
| 191 | return err |
| 192 | } |
| 193 | size := int(l) |
| 194 | for i := 0; i < size; i++ { |
| 195 | Skip(self, elemType, maxDepth-1) |
| 196 | } |
| 197 | return self.ReadListEnd() |
| 198 | } |
| 199 | } |
| 200 | return nil |
| 201 | } |