ToBytes converts a string, int, float or bool to a byte representation.
(in interface{})
| 968 | |
| 969 | // ToBytes converts a string, int, float or bool to a byte representation. |
| 970 | func ToBytes(in interface{}) ([]byte, error) { |
| 971 | switch in := in.(type) { |
| 972 | case string: |
| 973 | return []byte(in), nil |
| 974 | case int: |
| 975 | return []byte(strconv.Itoa(in)), nil |
| 976 | case float64: |
| 977 | return []byte(strconv.FormatFloat(in, 'f', -1, 64)), nil |
| 978 | case bool: |
| 979 | boolB := []byte("True") |
| 980 | if !in { |
| 981 | boolB = []byte("False") |
| 982 | } |
| 983 | return boolB, nil |
| 984 | case []byte: |
| 985 | return in, nil |
| 986 | case time.Time: |
| 987 | return in.MarshalText() |
| 988 | case Comment: |
| 989 | return ToBytes(in.Value) |
| 990 | default: |
| 991 | return nil, fmt.Errorf("Could not convert unknown type %T to bytes", in) |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | // EmitAsMap will emit the tree branches as a map. This is used by the publish |
| 996 | // command for writing decrypted trees to various destinations. Should only be |