EncodeText reads thrift data with descriptor, and converts it to a specail text-protocol string: This protocol is similar to JSON, excepts its key (or field id) IS NOT QUOTED unless it is a string type: - LIST/SET's all elements will be joined with ',', and if asJson is true the entiry value will be
(desc *TypeDescriptor, buf *[]byte, byteAsUint8 bool, disallowUnknown bool, base64Binary bool, useFieldName bool, asJson bool)
| 832 | // and if asJson is true the entiry value will be wrapped by '{' (start) and '}' (end). |
| 833 | // - STRING (including key) will be wrapped by '"' if asJson is true. |
| 834 | func (p *BinaryProtocol) EncodeText(desc *TypeDescriptor, buf *[]byte, byteAsUint8 bool, disallowUnknown bool, base64Binary bool, useFieldName bool, asJson bool) error { |
| 835 | switch desc.Type() { |
| 836 | case BOOL: |
| 837 | b, err := p.ReadBool() |
| 838 | if err != nil { |
| 839 | return err |
| 840 | } |
| 841 | *buf = strconv.AppendBool(*buf, b) |
| 842 | return nil |
| 843 | case BYTE: |
| 844 | b, err := p.ReadByte() |
| 845 | if err != nil { |
| 846 | return err |
| 847 | } |
| 848 | if byteAsUint8 { |
| 849 | *buf = strconv.AppendInt(*buf, int64(uint8(b)), 10) |
| 850 | return nil |
| 851 | } else { |
| 852 | *buf = strconv.AppendInt(*buf, int64(b), 10) |
| 853 | return nil |
| 854 | } |
| 855 | case I16: |
| 856 | i, err := p.ReadI16() |
| 857 | if err != nil { |
| 858 | return err |
| 859 | } |
| 860 | *buf = json.EncodeInt64(*buf, int64(i)) |
| 861 | return nil |
| 862 | case I32: |
| 863 | i, err := p.ReadI32() |
| 864 | if err != nil { |
| 865 | return err |
| 866 | } |
| 867 | *buf = json.EncodeInt64(*buf, int64(i)) |
| 868 | return nil |
| 869 | case I64: |
| 870 | i, err := p.ReadI64() |
| 871 | if err != nil { |
| 872 | return err |
| 873 | } |
| 874 | *buf = json.EncodeInt64(*buf, i) |
| 875 | return nil |
| 876 | case DOUBLE: |
| 877 | f, err := p.ReadDouble() |
| 878 | if err != nil { |
| 879 | return err |
| 880 | } |
| 881 | *buf = json.EncodeFloat64(*buf, f) |
| 882 | return nil |
| 883 | case STRING: |
| 884 | if base64Binary && desc.IsBinary() { |
| 885 | vs, err := p.ReadBinary(false) |
| 886 | if err != nil { |
| 887 | return err |
| 888 | } |
| 889 | if !asJson { |
| 890 | *buf = json.EncodeBase64(*buf, vs) |
| 891 | return nil |
no test coverage detected