| 1 |
// Copyright 2012-2023 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package builtin |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"fmt" |
| 19 |
"reflect" |
| 20 |
"strconv" |
| 21 |
"unsafe" |
| 22 |
) |
| 23 |
|
| 24 |
// DefaultEncoder implementation for EncodedConn. |
| 25 |
// This encoder will leave []byte and string untouched, but will attempt to |
| 26 |
// turn numbers into appropriate strings that can be decoded. It will also |
| 27 |
// properly encoded and decode bools. If will encode a struct, but if you want |
| 28 |
// to properly handle structures you should use JsonEncoder. |
| 29 |
// |
| 30 |
// Deprecated: Encoded connections are no longer supported. |
| 31 |
type DefaultEncoder struct { |
| 32 |
// Empty |
| 33 |
} |
| 34 |
|
| 35 |
var trueB = []byte("true") |
| 36 |
var falseB = []byte("false") |
| 37 |
var nilB = []byte("") |
| 38 |
|
| 39 |
// Encode |
| 40 |
// |
| 41 |
// Deprecated: Encoded connections are no longer supported. |
| 42 |
func (je *DefaultEncoder) Encode(subject string, v any) ([]byte, error) { |
| 43 |
switch arg := v.(type) { |
| 44 |
case string: |
| 45 |
bytes := *(*[]byte)(unsafe.Pointer(&arg)) |
| 46 |
return bytes, nil |
| 47 |
case []byte: |
| 48 |
return arg, nil |
| 49 |
case bool: |
| 50 |
if arg { |
| 51 |
return trueB, nil |
| 52 |
} else { |
| 53 |
return falseB, nil |
| 54 |
} |
| 55 |
case nil: |
| 56 |
return nilB, nil |
| 57 |
default: |
| 58 |
var buf bytes.Buffer |
| 59 |
fmt.Fprintf(&buf, "%+v", arg) |
| 60 |
return buf.Bytes(), nil |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// Decode |
| 65 |
// |
| 66 |
// Deprecated: Encoded connections are no longer supported. |
| 67 |
func (je *DefaultEncoder) Decode(subject string, data []byte, vPtr any) error { |
| 68 |
// Figure out what it's pointing to... |
| 69 |
sData := *(*string)(unsafe.Pointer(&data)) |
| 70 |
switch arg := vPtr.(type) { |
| 71 |
case *string: |
| 72 |
*arg = sData |
| 73 |
return nil |
| 74 |
case *[]byte: |
| 75 |
*arg = data |
| 76 |
return nil |
| 77 |
case *int: |
| 78 |
n, err := strconv.ParseInt(sData, 10, 64) |
| 79 |
if err != nil { |
| 80 |
return err |
| 81 |
} |
| 82 |
*arg = int(n) |
| 83 |
return nil |
| 84 |
case *int32: |
| 85 |
n, err := strconv.ParseInt(sData, 10, 64) |
| 86 |
if err != nil { |
| 87 |
return err |
| 88 |
} |
| 89 |
*arg = int32(n) |
| 90 |
return nil |
| 91 |
case *int64: |
| 92 |
n, err := strconv.ParseInt(sData, 10, 64) |
| 93 |
if err != nil { |
| 94 |
return err |
| 95 |
} |
| 96 |
*arg = int64(n) |
| 97 |
return nil |
| 98 |
case *float32: |
| 99 |
n, err := strconv.ParseFloat(sData, 32) |
| 100 |
if err != nil { |
| 101 |
return err |
| 102 |
} |
| 103 |
*arg = float32(n) |
| 104 |
return nil |
| 105 |
case *float64: |
| 106 |
n, err := strconv.ParseFloat(sData, 64) |
| 107 |
if err != nil { |
| 108 |
return err |
| 109 |
} |
| 110 |
*arg = float64(n) |
| 111 |
return nil |
| 112 |
case *bool: |
| 113 |
b, err := strconv.ParseBool(sData) |
| 114 |
if err != nil { |
| 115 |
return err |
| 116 |
} |
| 117 |
*arg = b |
| 118 |
return nil |
| 119 |
default: |
| 120 |
vt := reflect.TypeOf(arg).Elem() |
| 121 |
return fmt.Errorf("nats: Default Encoder can't decode to type %s", vt) |
| 122 |
} |
| 123 |
} |