ppforge/protocol/field.go

59 lines
1.2 KiB
Go
Raw Permalink Normal View History

package protocol
import "encoding/json"
2023-11-10 13:48:47 +01:00
// Field holds all necessary data for a field in a protocol
type Field struct {
Name string // Name of the Field
Desc string // Lengthy description
Regex string // Regex to recognize values
Size int // Size in bits!
SubFields []Field // Possible sub-fields
Optional bool // Is this field required?
Payload bool // Is this field the payload or next protocol level?
JavaScript string
}
2023-11-10 13:48:47 +01:00
// FieldReferencer interface
type FieldReferencer interface {
2023-04-11 16:05:24 +02:00
GetProtocolField() *Field
SetProtocolField(f *Field)
}
2023-11-10 13:48:47 +01:00
// ToJSON converts a field to a JSON string
func (f *Field) ToJSON() string {
b, err := json.Marshal(*f)
if err != nil {
return ""
}
return string(b)
}
2023-11-10 13:48:47 +01:00
// NewEmptyField just creates a new Field with no data
func NewEmptyField() *Field {
f := Field{}
return &f
}
2023-11-10 13:48:47 +01:00
// NewField creates a new Field with all data possible
func NewField(
name string,
desc string,
regex string,
size int,
subfields []Field,
optional bool,
payload bool,
) *Field {
f := Field{
Name: name,
Desc: desc,
Regex: regex,
Size: size,
SubFields: subfields,
Optional: optional,
Payload: payload,
}
return &f
}