Some refactoring of packet.go and proper comments
Go / lint (push) Failing after 9s Details
Go / build (push) Failing after 10s Details

This commit is contained in:
Marcel M. Otte 2023-11-10 17:35:36 +01:00
parent a89787613e
commit 1daf8f01e1
4 changed files with 22 additions and 27 deletions

View File

@ -12,26 +12,9 @@ import (
// FieldValue implements protocol.ProtocolFieldReferencer // FieldValue implements protocol.ProtocolFieldReferencer
type FieldValue struct { type FieldValue struct {
Field string
FieldNum int
Value string Value string
}
// GetProtocolField returns the protocol field Generate bool
func (fv *FieldValue) GetProtocolField() (string, int) {
return fv.Field, fv.FieldNum
}
// SetProtocolField sets the protocl field
func (fv *FieldValue) SetProtocolField(f string, fn int) {
fv.Field = f
fv.FieldNum = fn
}
// SetValue sets the value
func (fv *FieldValue) SetValue(v string) {
fv.Value = v
} }
// Layer implements protocol.ProtocolReferencer // Layer implements protocol.ProtocolReferencer
@ -50,29 +33,33 @@ type Packet struct {
Layers []Layer Layers []Layer
} }
// NewPacketStructure creates a new empty packet structure
func NewPacketStructure() *Packet { func NewPacketStructure() *Packet {
p := Packet{} p := Packet{}
return &p return &p
} }
// UpdateMetaData is still a method stub
func UpdateMetaData( func UpdateMetaData(
pack *Packet, pack *Packet,
) { ) {
// still empty // still empty
} }
// NewPacketLayer creates a new empty Layer structure
func NewPacketLayer() *Layer { func NewPacketLayer() *Layer {
p := Layer{} p := Layer{}
return &p return &p
} }
// NewEmptyFieldValue creates a new empty field value
func NewEmptyFieldValue() *FieldValue { func NewEmptyFieldValue() *FieldValue {
f := FieldValue{} f := FieldValue{}
return &f return &f
} }
// NewFieldValue creates a new prefilled field value
func NewFieldValue( func NewFieldValue(
field string,
fieldnum int,
value string, value string,
) *FieldValue { ) *FieldValue {
f := FieldValue{ f := FieldValue{
@ -81,12 +68,14 @@ func NewFieldValue(
return &f return &f
} }
// AppendField appends a new FieldValue to the Layer
func (pack *Layer) AppendField(field *FieldValue) int { func (pack *Layer) AppendField(field *FieldValue) int {
i := len(pack.Values) i := len(pack.Values)
pack.Values = append(pack.Values, *field) pack.Values = append(pack.Values, *field)
return i + 1 return i + 1
} }
// AddField adds a FieldValue at any position
func (pack *Layer) AddField(index int, field *FieldValue) { func (pack *Layer) AddField(index int, field *FieldValue) {
if len(pack.Values) == index { if len(pack.Values) == index {
pack.AppendField(field) pack.AppendField(field)
@ -99,10 +88,12 @@ func (pack *Layer) AddField(index int, field *FieldValue) {
pack.Values = ret pack.Values = ret
} }
// AppendLayer appends a Layer to the list of Layers
func (pack *Packet) AppendLayer(layer *Layer) { func (pack *Packet) AppendLayer(layer *Layer) {
pack.Layers = append(pack.Layers, *layer) pack.Layers = append(pack.Layers, *layer)
} }
// AddLayer adds a Layer at any position
func (pack *Packet) AddLayer(index int, layer *Layer) { func (pack *Packet) AddLayer(index int, layer *Layer) {
if len(pack.Layers) == index { if len(pack.Layers) == index {
pack.AppendLayer(layer) pack.AppendLayer(layer)
@ -115,14 +106,17 @@ func (pack *Packet) AddLayer(index int, layer *Layer) {
pack.Layers = ret pack.Layers = ret
} }
// UpdateField updates a field of a layer
func (pack *Layer) UpdateField(e int, field *FieldValue) { func (pack *Layer) UpdateField(e int, field *FieldValue) {
pack.Values[e] = *field pack.Values[e] = *field
} }
// UpdateLayer updates a Layer with a reference to a new Layer
func (pack *Packet) UpdateLayer(e int, layer *Layer) { func (pack *Packet) UpdateLayer(e int, layer *Layer) {
pack.Layers[e] = *layer pack.Layers[e] = *layer
} }
// RemoveField removes a field from a Layer
func (pack *Layer) RemoveField(e int) { func (pack *Layer) RemoveField(e int) {
l := len(pack.Values) - 1 l := len(pack.Values) - 1
ret := make([]FieldValue, l) ret := make([]FieldValue, l)
@ -131,6 +125,7 @@ func (pack *Layer) RemoveField(e int) {
pack.Values = ret pack.Values = ret
} }
// RemoveLayer removes a layer from the list at specified index
func (pack *Packet) RemoveLayer(e int) { func (pack *Packet) RemoveLayer(e int) {
l := len(pack.Layers) - 1 l := len(pack.Layers) - 1
ret := make([]Layer, l) ret := make([]Layer, l)
@ -139,6 +134,7 @@ func (pack *Packet) RemoveLayer(e int) {
pack.Layers = ret pack.Layers = ret
} }
// Load a packet from a file path
func (pack *Packet) Load(path string) error { func (pack *Packet) Load(path string) error {
data, err := os.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
@ -155,12 +151,14 @@ func (pack *Packet) Load(path string) error {
return err return err
} }
// UpdateData updates the data holding elements with the actual hex/b64 binary representations and a sha1 hash of it.
func (pack *Packet) UpdateData() { func (pack *Packet) UpdateData() {
pack.Hex = fmt.Sprintf("%x", pack.data) pack.Hex = fmt.Sprintf("%x", pack.data)
pack.B64 = base64.RawStdEncoding.EncodeToString(pack.data) pack.B64 = base64.RawStdEncoding.EncodeToString(pack.data)
pack.Sha1 = fmt.Sprintf("%x", sha1.Sum(pack.data)) pack.Sha1 = fmt.Sprintf("%x", sha1.Sum(pack.data))
} }
// ToJSON returns a JSON string of the packet structure
func (pack *Packet) ToJSON() string { func (pack *Packet) ToJSON() string {
pack.UpdateData() pack.UpdateData()
data, err := json.MarshalIndent(*pack, "", " ") data, err := json.MarshalIndent(*pack, "", " ")
@ -170,6 +168,7 @@ func (pack *Packet) ToJSON() string {
return string(data) return string(data)
} }
// Save the packet structure as JSON file at specified path
func (pack *Packet) Save(path string) error { func (pack *Packet) Save(path string) error {
pack.UpdateData() pack.UpdateData()
data, err := json.MarshalIndent(*pack, "", " ") data, err := json.MarshalIndent(*pack, "", " ")

View File

@ -1,7 +0,0 @@
package packet
// Meta struct is the absolute minimum of metadata
type Meta struct {
Name string
Revision int
}

View File

@ -1,5 +1,6 @@
package protocol package protocol
// [impl->dsn~protocol-data-architecture~0>>utest]
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"

View File

@ -1,5 +1,7 @@
package protocol package protocol
// [utest->dsn~protocol-data-architecture~0]
// [utest->impl~protocol-data-architecture~0]
import ( import (
"fmt" "fmt"
"path" "path"