| 1 |
// Copyright 2013-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 |
"encoding/gob" |
| 19 |
) |
| 20 |
|
| 21 |
// GobEncoder is a Go specific GOB Encoder implementation for EncodedConn. |
| 22 |
// This encoder will use the builtin encoding/gob to Marshal |
| 23 |
// and Unmarshal most types, including structs. |
| 24 |
// |
| 25 |
// Deprecated: Encoded connections are no longer supported. |
| 26 |
type GobEncoder struct { |
| 27 |
// Empty |
| 28 |
} |
| 29 |
|
| 30 |
// FIXME(dlc) - This could probably be more efficient. |
| 31 |
|
| 32 |
// Encode |
| 33 |
// |
| 34 |
// Deprecated: Encoded connections are no longer supported. |
| 35 |
func (ge *GobEncoder) Encode(subject string, v any) ([]byte, error) { |
| 36 |
b := new(bytes.Buffer) |
| 37 |
enc := gob.NewEncoder(b) |
| 38 |
if err := enc.Encode(v); err != nil { |
| 39 |
return nil, err |
| 40 |
} |
| 41 |
return b.Bytes(), nil |
| 42 |
} |
| 43 |
|
| 44 |
// Decode |
| 45 |
// |
| 46 |
// Deprecated: Encoded connections are no longer supported. |
| 47 |
func (ge *GobEncoder) Decode(subject string, data []byte, vPtr any) (err error) { |
| 48 |
dec := gob.NewDecoder(bytes.NewBuffer(data)) |
| 49 |
err = dec.Decode(vPtr) |
| 50 |
return |
| 51 |
} |