-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Packer and Unpacker to the field spec
- Loading branch information
Showing
9 changed files
with
133 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package field | ||
|
||
import "fmt" | ||
|
||
type DefaultPacker struct{} | ||
|
||
func (p DefaultPacker) Pack(data []byte, spec *Spec) ([]byte, error) { | ||
if spec.Pad != nil { | ||
data = spec.Pad.Pad(data, spec.Length) | ||
} | ||
|
||
packed, err := spec.Enc.Encode(data) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to encode content: %w", err) | ||
} | ||
|
||
packedLength, err := spec.Pref.EncodeLength(spec.Length, len(data)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to encode length: %w", err) | ||
} | ||
|
||
return append(packedLength, packed...), nil | ||
} | ||
|
||
type DefaultUnpacker struct{} | ||
|
||
func (u DefaultUnpacker) Unpack(data []byte, spec *Spec) ([]byte, int, error) { | ||
dataLen, prefBytes, err := spec.Pref.DecodeLength(spec.Length, data) | ||
if err != nil { | ||
return nil, 0, fmt.Errorf("failed to decode length: %w", err) | ||
} | ||
|
||
raw, read, err := spec.Enc.Decode(data[prefBytes:], dataLen) | ||
if err != nil { | ||
return nil, 0, fmt.Errorf("failed to decode content: %w", err) | ||
} | ||
|
||
if spec.Pad != nil { | ||
raw = spec.Pad.Unpad(raw) | ||
} | ||
|
||
return raw, read + prefBytes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.