BeEncode encodes one or multiple `values` into bytes using BigEndian. It uses type asserting checking the type of each value of `values` and internally calls corresponding converting function do the bytes converting. It supports common variable type asserting, and finally it uses fmt.Sprintf conver
(values ...any)
| 24 | // It supports common variable type asserting, and finally it uses fmt.Sprintf converting |
| 25 | // value to string and then to bytes. |
| 26 | func BeEncode(values ...any) []byte { |
| 27 | buf := new(bytes.Buffer) |
| 28 | for i := 0; i < len(values); i++ { |
| 29 | if values[i] == nil { |
| 30 | return buf.Bytes() |
| 31 | } |
| 32 | |
| 33 | switch value := values[i].(type) { |
| 34 | case int: |
| 35 | buf.Write(BeEncodeInt(value)) |
| 36 | case int8: |
| 37 | buf.Write(BeEncodeInt8(value)) |
| 38 | case int16: |
| 39 | buf.Write(BeEncodeInt16(value)) |
| 40 | case int32: |
| 41 | buf.Write(BeEncodeInt32(value)) |
| 42 | case int64: |
| 43 | buf.Write(BeEncodeInt64(value)) |
| 44 | case uint: |
| 45 | buf.Write(BeEncodeUint(value)) |
| 46 | case uint8: |
| 47 | buf.Write(BeEncodeUint8(value)) |
| 48 | case uint16: |
| 49 | buf.Write(BeEncodeUint16(value)) |
| 50 | case uint32: |
| 51 | buf.Write(BeEncodeUint32(value)) |
| 52 | case uint64: |
| 53 | buf.Write(BeEncodeUint64(value)) |
| 54 | case bool: |
| 55 | buf.Write(BeEncodeBool(value)) |
| 56 | case string: |
| 57 | buf.Write(BeEncodeString(value)) |
| 58 | case []byte: |
| 59 | buf.Write(value) |
| 60 | case float32: |
| 61 | buf.Write(BeEncodeFloat32(value)) |
| 62 | case float64: |
| 63 | buf.Write(BeEncodeFloat64(value)) |
| 64 | default: |
| 65 | if err := binary.Write(buf, binary.BigEndian, value); err != nil { |
| 66 | intlog.Errorf(context.TODO(), `%+v`, err) |
| 67 | buf.Write(BeEncodeString(fmt.Sprintf("%v", value))) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | return buf.Bytes() |
| 72 | } |
| 73 | |
| 74 | func BeEncodeByLength(length int, values ...any) []byte { |
| 75 | b := BeEncode(values...) |
searching dependent graphs…