linter fixes and test fixes
lint Details
build Details

This commit is contained in:
Marcel M. Otte 2023-05-12 17:18:24 +02:00
parent 204da1c880
commit c9fcb3d947
8 changed files with 48 additions and 21 deletions

View File

@ -9,6 +9,7 @@ import (
var fyneApp fyne.App var fyneApp fyne.App
var w fyne.Window var w fyne.Window
// Appmain creates the Fyne application and starts it up
func Appmain() { func Appmain() {
fyneApp = app.New() fyneApp = app.New()
w = fyneApp.NewWindow("ProtocolPacketForger") w = fyneApp.NewWindow("ProtocolPacketForger")
@ -19,6 +20,7 @@ func Appmain() {
w.ShowAndRun() w.ShowAndRun()
} }
// CreateApp creates the PPF UI
func CreateApp() *fyne.Container { func CreateApp() *fyne.Container {
ppf := ui.NewPPF(fyneApp, w) ppf := ui.NewPPF(fyneApp, w)
return ppf.GetContainer() return ppf.GetContainer()

View File

@ -5,10 +5,11 @@ import (
"gitea.mmo.to/ppForge/ppforge/protocolctl" "gitea.mmo.to/ppForge/ppforge/protocolctl"
) )
// GetAdder returns a fyne button which on click creates a new field in the editor
func GetAdder(pEd *ProtocolEditor) *widget.Button { func GetAdder(pEd *ProtocolEditor) *widget.Button {
fieldAdder := widget.NewButton("Add Field", func() { fieldAdder := widget.NewButton("Add Field", func() {
f := protocolctl.NewEmptyField() f := protocolctl.NewEmptyField()
index := protocolctl.AppendField(pEd.Reference, f) index := protocolctl.AppendField(pEd.Reference, *f)
pEd.AddFieldCreator(CreateFieldEditor(pEd, index, f)) pEd.AddFieldCreator(CreateFieldEditor(pEd, index, f))
}) })
return fieldAdder return fieldAdder

View File

@ -13,6 +13,7 @@ import (
"gitea.mmo.to/ppForge/ppforge/protocolctl" "gitea.mmo.to/ppForge/ppforge/protocolctl"
) )
// FieldEditor is a struct holding all elements of the field editor ui
type FieldEditor struct { type FieldEditor struct {
Representation *fyne.Container Representation *fyne.Container
NameValue *widget.Entry NameValue *widget.Entry
@ -29,6 +30,7 @@ type FieldEditor struct {
Index int Index int
} }
// CreateFieldEditor initializes a new field editor ui for an editor and a protocol
func CreateFieldEditor(ed *ProtocolEditor, index int, ref *protocol.Field) *FieldEditor { func CreateFieldEditor(ed *ProtocolEditor, index int, ref *protocol.Field) *FieldEditor {
fc := &FieldEditor{} fc := &FieldEditor{}
fc.Reference = ref fc.Reference = ref

View File

@ -36,8 +36,8 @@ type TabProvider interface {
Tab() *container.TabItem Tab() *container.TabItem
} }
// ppfApp is the internal struct holding all the required main objects/elements for the application // PpfApp is the internal struct holding all the required main objects/elements for the application
type ppfApp struct { type PpfApp struct {
App fyne.App App fyne.App
Window fyne.Window Window fyne.Window
// Container for the main layout, toolbar at top, in Center the doctabs. // Container for the main layout, toolbar at top, in Center the doctabs.
@ -70,10 +70,10 @@ type ppfApp struct {
} }
// PPF is the global variable holding the application struct // PPF is the global variable holding the application struct
var PPF ppfApp var PPF PpfApp
// NewPPF is the entrypoint to create the application // NewPPF is the entrypoint to create the application
func NewPPF(fyneApp fyne.App, w fyne.Window) ppfApp { func NewPPF(fyneApp fyne.App, w fyne.Window) PpfApp {
PPF.App = fyneApp PPF.App = fyneApp
PPF.Window = w PPF.Window = w
PPF.Toolbar = CreateToolbar(fyneApp) PPF.Toolbar = CreateToolbar(fyneApp)
@ -81,7 +81,8 @@ func NewPPF(fyneApp fyne.App, w fyne.Window) ppfApp {
return PPF return PPF
} }
func (ppf *ppfApp) GetContainer() *fyne.Container { // GetContainer delivers the main container of the application
func (ppf *PpfApp) GetContainer() *fyne.Container {
return container.NewBorder( return container.NewBorder(
ppf.Toolbar, //top ppf.Toolbar, //top
nil, //bottom nil, //bottom
@ -91,7 +92,8 @@ func (ppf *ppfApp) GetContainer() *fyne.Container {
) )
} }
func (ppf *ppfApp) NewProtocolFile() { // NewProtocolFile creates and shows a new protocol file
func (ppf *PpfApp) NewProtocolFile() {
//TODO: show entry screen with choosable ways of: //TODO: show entry screen with choosable ways of:
// PROTOCOL editor // PROTOCOL editor
// create a new protocol // create a new protocol
@ -107,11 +109,13 @@ func (ppf *ppfApp) NewProtocolFile() {
pfh.SetTab(item) pfh.SetTab(item)
} }
func (ppf *ppfApp) NewPacketFile() { // NewPacketFile creates and shows a new packet file
func (ppf *PpfApp) NewPacketFile() {
} }
func (ppf *ppfApp) OpenProtocolFile(path string) { // OpenProtocolFile opens and shows a protocol file
func (ppf *PpfApp) OpenProtocolFile(path string) {
for _, v := range ppf.OpenTabs.Items { for _, v := range ppf.OpenTabs.Items {
if v.Text == path { if v.Text == path {
// do not open a file twice! // do not open a file twice!
@ -128,11 +132,13 @@ func (ppf *ppfApp) OpenProtocolFile(path string) {
pfh.SetTab(item) pfh.SetTab(item)
} }
func (ppf *ppfApp) OpenPacketFile(path string) { // OpenPacketFile opens and shows a packet file
func (ppf *PpfApp) OpenPacketFile(path string) {
} }
func (ppf *ppfApp) SaveFile(path string) { // SaveFile saves both a protocol and a packet file
func (ppf *PpfApp) SaveFile(path string) {
item := ppf.OpenTabs.Selected() item := ppf.OpenTabs.Selected()
for _, fh := range ppf.OpenObjects { for _, fh := range ppf.OpenObjects {
if fh.(TabProvider).Tab() == item { if fh.(TabProvider).Tab() == item {
@ -158,7 +164,8 @@ func (ppf *ppfApp) SaveFile(path string) {
ppf.OpenTabs.Refresh() ppf.OpenTabs.Refresh()
} }
func (ppf *ppfApp) NewFile() { // NewFile shows the new item dialog
func (ppf *PpfApp) NewFile() {
for _, v := range ppf.OpenTabs.Items { for _, v := range ppf.OpenTabs.Items {
if v.Text == "[new item dialog]" { if v.Text == "[new item dialog]" {
ppf.OpenTabs.Select(v) ppf.OpenTabs.Select(v)
@ -170,6 +177,7 @@ func (ppf *ppfApp) NewFile() {
ppf.OpenTabs.Select(item) ppf.OpenTabs.Select(item)
} }
// CreateToolbar initializes the toolbar
func CreateToolbar(fyneApp fyne.App) fyne.CanvasObject { func CreateToolbar(fyneApp fyne.App) fyne.CanvasObject {
toolbar := widget.NewToolbar() toolbar := widget.NewToolbar()
toolbar.Append(widget.NewToolbarAction(theme.ContentAddIcon(), func() { toolbar.Append(widget.NewToolbarAction(theme.ContentAddIcon(), func() {
@ -207,6 +215,7 @@ func CreateToolbar(fyneApp fyne.App) fyne.CanvasObject {
return toolbar return toolbar
} }
// CreateWorkarea initializes the workarea
func CreateWorkarea() *container.DocTabs { func CreateWorkarea() *container.DocTabs {
tabs := container.NewDocTabs() tabs := container.NewDocTabs()
tabs.OnClosed = func(ti *container.TabItem) { tabs.OnClosed = func(ti *container.TabItem) {

View File

@ -9,10 +9,12 @@ type FieldValue struct {
Value string Value string
} }
// GetProtocolField returns the protocol field
func (fv *FieldValue) GetProtocolField() *protocol.Field { func (fv *FieldValue) GetProtocolField() *protocol.Field {
return fv.fieldReference return fv.fieldReference
} }
// SetProtocolField sets the protocl field
func (fv *FieldValue) SetProtocolField(pf *protocol.Field) { func (fv *FieldValue) SetProtocolField(pf *protocol.Field) {
fv.fieldReference = pf fv.fieldReference = pf
} }

View File

@ -1,5 +1,6 @@
package packet package packet
// Metadata struct is the absolute minimum of metadata
type Metadata struct { type Metadata struct {
Name string Name string
} }

View File

@ -2,6 +2,7 @@ package protocolctl
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io/fs" "io/fs"
"os" "os"
@ -56,14 +57,14 @@ func NewField(
return &f return &f
} }
func AppendField(prot *protocol.ProtocolStructure, field *protocol.Field) int { func AppendField(prot *protocol.ProtocolStructure, field protocol.Field) int {
i := len(prot.Structure) i := len(prot.Structure)
prot.Structure = append(prot.Structure, field) prot.Structure = append(prot.Structure, &field)
return i + 1 return i + 1
} }
func AddField(prot *protocol.ProtocolStructure, index int, field *protocol.Field) { func AddField(prot *protocol.ProtocolStructure, index int, field *protocol.Field) {
if len(prot.Structure) == index { if len(prot.Structure) == index {
AppendField(prot, field) AppendField(prot, *field)
return return
} }
ret := make([]*protocol.Field, 0) ret := make([]*protocol.Field, 0)
@ -104,8 +105,12 @@ func RemoveFieldByName(prot *protocol.ProtocolStructure, name string) {
func RemoveFieldByElement(prot *protocol.ProtocolStructure, field int) { func RemoveFieldByElement(prot *protocol.ProtocolStructure, field int) {
ret := make([]*protocol.Field, 0) ret := make([]*protocol.Field, 0)
ret = append(ret, prot.Structure[:field]...) for i, f := range prot.Structure {
ret = append(ret, prot.Structure[field+1:]...) if i != field {
fmt.Printf("appending %d, %s\n", i, f.ToJson())
ret = append(ret, f)
}
}
prot.Structure = ret prot.Structure = ret
} }

View File

@ -43,7 +43,7 @@ func TestUpdateMetaData(t *testing.T) {
func GenerateFields() []protocol.Field { func GenerateFields() []protocol.Field {
slc := make([]protocol.Field, 0) slc := make([]protocol.Field, 0)
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
slc = append(slc, *NewField( field := NewField(
fmt.Sprintf("testfield%d", i), fmt.Sprintf("testfield%d", i),
fmt.Sprintf("Description %d", i), fmt.Sprintf("Description %d", i),
"", "",
@ -51,7 +51,8 @@ func GenerateFields() []protocol.Field {
nil, nil,
false, false,
false, false,
)) )
slc = append(slc, *field)
} }
return slc return slc
} }
@ -60,9 +61,13 @@ func GenerateStructure() *protocol.ProtocolStructure {
p := NewProtocolStructure() p := NewProtocolStructure()
slc := GenerateFields() slc := GenerateFields()
for i, e := range slc { for i, e := range slc {
AppendField(p, &e) AppendField(p, e)
fmt.Printf("%d %s\n", i, e.ToJson()) fmt.Printf("%d %s\n", i, e.ToJson())
} }
for i, e := range p.Structure {
fmt.Printf("%d %s\n", i, e.ToJson())
}
fmt.Println("Generated Structure.")
return p return p
} }
@ -77,7 +82,7 @@ func TestFieldAppend(t *testing.T) {
false, false,
) )
p := NewProtocolStructure() p := NewProtocolStructure()
AppendField(p, f) AppendField(p, *f)
if p.Structure[0].Name != "testfield" { if p.Structure[0].Name != "testfield" {
t.Log("Append failed.") t.Log("Append failed.")
t.Fail() t.Fail()