github.com/segmentio/encoding@v0.4.0/thrift/thrift.go (about)

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