1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
package thrift
import (
"fmt"
"reflect"
)
type Message struct {
Type MessageType
Name string
SeqID int32
}
type MessageType int8
const (
Call MessageType = iota
Reply
Exception
Oneway
)
func (m MessageType) String() string {
switch m {
case Call:
return "Call"
case Reply:
return "Reply"
case Exception:
return "Exception"
case Oneway:
return "Oneway"
default:
return "?"
}
}
type Field struct {
ID int16
Type Type
Delta bool // whether the field id is a delta
}
func (f Field) String() string {
return fmt.Sprintf("%d:FIELD<%s>", f.ID, f.Type)
}
type Type int8
const (
STOP Type = iota
TRUE
FALSE
I8
I16
I32
I64
DOUBLE
BINARY
LIST
SET
MAP
STRUCT
BOOL = FALSE
)
func (t Type) String() string {
switch t {
case STOP:
return "STOP"
case TRUE:
return "TRUE"
case BOOL:
return "BOOL"
case I8:
return "I8"
case I16:
return "I16"
case I32:
return "I32"
case I64:
return "I64"
case DOUBLE:
return "DOUBLE"
case BINARY:
return "BINARY"
case LIST:
return "LIST"
case SET:
return "SET"
case MAP:
return "MAP"
case STRUCT:
return "STRUCT"
default:
return "?"
}
}
func (t Type) GoString() string {
return "thrift." + t.String()
}
type List struct {
Size int32
Type Type
}
func (l List) String() string {
return fmt.Sprintf("LIST<%s>", l.Type)
}
type Set List
func (s Set) String() string {
return fmt.Sprintf("SET<%s>", s.Type)
}
type Map struct {
Size int32
Key Type
Value Type
}
func (m Map) String() string {
return fmt.Sprintf("MAP<%s,%s>", m.Key, m.Value)
}
func TypeOf(t reflect.Type) Type {
switch t.Kind() {
case reflect.Bool:
return BOOL
case reflect.Int8, reflect.Uint8:
return I8
case reflect.Int16, reflect.Uint16:
return I16
case reflect.Int32, reflect.Uint32:
return I32
case reflect.Int64, reflect.Uint64, reflect.Int, reflect.Uint, reflect.Uintptr:
return I64
case reflect.Float32, reflect.Float64:
return DOUBLE
case reflect.String:
return BINARY
case reflect.Slice:
if t.Elem().Kind() == reflect.Uint8 { // []byte
return BINARY
} else {
return LIST
}
case reflect.Map:
if t.Elem().Size() == 0 {
return SET
} else {
return MAP
}
case reflect.Struct:
return STRUCT
case reflect.Ptr:
return TypeOf(t.Elem())
default:
panic("type cannot be represented in thrift: " + t.String())
}
}
|