LeEncode encodes one or multiple `values` into bytes using LittleEndian. 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 con
(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 LeEncode(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 | switch value := values[i].(type) { |
| 33 | case int: |
| 34 | buf.Write(LeEncodeInt(value)) |
| 35 | case int8: |
| 36 | buf.Write(LeEncodeInt8(value)) |
| 37 | case int16: |
| 38 | buf.Write(LeEncodeInt16(value)) |
| 39 | case int32: |
| 40 | buf.Write(LeEncodeInt32(value)) |
| 41 | case int64: |
| 42 | buf.Write(LeEncodeInt64(value)) |
| 43 | case uint: |
| 44 | buf.Write(LeEncodeUint(value)) |
| 45 | case uint8: |
| 46 | buf.Write(LeEncodeUint8(value)) |
| 47 | case uint16: |
| 48 | buf.Write(LeEncodeUint16(value)) |
| 49 | case uint32: |
| 50 | buf.Write(LeEncodeUint32(value)) |
| 51 | case uint64: |
| 52 | buf.Write(LeEncodeUint64(value)) |
| 53 | case bool: |
| 54 | buf.Write(LeEncodeBool(value)) |
| 55 | case string: |
| 56 | buf.Write(LeEncodeString(value)) |
| 57 | case []byte: |
| 58 | buf.Write(value) |
| 59 | case float32: |
| 60 | buf.Write(LeEncodeFloat32(value)) |
| 61 | case float64: |
| 62 | buf.Write(LeEncodeFloat64(value)) |
| 63 | |
| 64 | default: |
| 65 | if err := binary.Write(buf, binary.LittleEndian, value); err != nil { |
| 66 | intlog.Errorf(context.TODO(), `%+v`, err) |
| 67 | buf.Write(LeEncodeString(fmt.Sprintf("%v", value))) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | return buf.Bytes() |
| 72 | } |
| 73 | |
| 74 | func LeEncodeByLength(length int, values ...any) []byte { |
| 75 | b := LeEncode(values...) |
searching dependent graphs…