ppforge/protocol/field.go

59 lines
1.2 KiB
Go

package protocol
import "encoding/json"
// 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
}
// FieldReferencer interface
type FieldReferencer interface {
GetProtocolField() *Field
SetProtocolField(f *Field)
}
// 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)
}
// NewEmptyField just creates a new Field with no data
func NewEmptyField() *Field {
f := Field{}
return &f
}
// 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
}