2023-01-02 20:02:50 +01:00
|
|
|
package protocol
|
|
|
|
|
2023-09-17 12:49:41 +02:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Protocol struct {
|
|
|
|
Metadata ProtocolMeta
|
2023-01-04 13:38:16 +01:00
|
|
|
Structure []*Field
|
2023-01-02 20:02:50 +01:00
|
|
|
DefaultValues []DefaultValue
|
|
|
|
JavaScript string
|
|
|
|
}
|
2023-04-11 16:05:24 +02:00
|
|
|
|
2023-09-17 12:49:41 +02:00
|
|
|
func (p Protocol) ToJson() string {
|
|
|
|
data, err := json.MarshalIndent(p, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProtocolStructure() *Protocol {
|
|
|
|
p := Protocol{}
|
|
|
|
return &p
|
|
|
|
}
|
|
|
|
func (prot *Protocol) AppendField(field Field) int {
|
|
|
|
i := len(prot.Structure)
|
|
|
|
prot.Structure = append(prot.Structure, &field)
|
|
|
|
return i + 1
|
|
|
|
}
|
|
|
|
func (prot *Protocol) AddField(index int, field *Field) {
|
|
|
|
if len(prot.Structure) == index {
|
|
|
|
prot.AppendField(*field)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret := make([]*Field, 0)
|
|
|
|
ret = append(ret, prot.Structure[:index]...)
|
|
|
|
ret = append(ret, field)
|
|
|
|
ret = append(ret, prot.Structure[index:]...)
|
|
|
|
prot.Structure = ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (prot *Protocol) UpdateFieldByName(name string, field *Field) {
|
|
|
|
var fnd int = -1
|
|
|
|
for i, f := range prot.Structure {
|
|
|
|
if f.Name == name {
|
|
|
|
fnd = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if fnd != -1 {
|
|
|
|
prot.Structure[fnd] = field
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (prot *Protocol) UpdateFieldByElement(element int, field *Field) {
|
|
|
|
prot.Structure[element] = field
|
|
|
|
}
|
|
|
|
|
|
|
|
func (prot *Protocol) RemoveFieldByName(name string) {
|
|
|
|
element := -1
|
|
|
|
for i, f := range prot.Structure {
|
|
|
|
if f.Name == name {
|
|
|
|
element = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if element == -1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
prot.RemoveFieldByElement(element)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (prot *Protocol) RemoveFieldByElement(field int) {
|
|
|
|
ret := make([]*Field, 0)
|
|
|
|
for i, f := range prot.Structure {
|
|
|
|
if i != field {
|
|
|
|
fmt.Printf("appending %d, %s\n", i, f.ToJson())
|
|
|
|
ret = append(ret, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prot.Structure = ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (prot *Protocol) Load(path string) error {
|
|
|
|
data, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(data, prot)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
func (prot *Protocol) Save(path string) error {
|
|
|
|
data, err := json.MarshalIndent(*prot, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.WriteFile(path, data, fs.ModeAppend)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadNew(path string) (*Protocol, error) {
|
|
|
|
prot := NewProtocolStructure()
|
|
|
|
err := prot.Load(path)
|
|
|
|
return prot, err
|
2023-04-11 16:05:24 +02:00
|
|
|
}
|