From c9fcb3d947fcf46bbeee6e1de2b04e40b15b3a2c Mon Sep 17 00:00:00 2001 From: "Marcel M. Otte" Date: Fri, 12 May 2023 17:18:24 +0200 Subject: [PATCH] linter fixes and test fixes --- internal/app/app.go | 2 ++ internal/ui/fieldadd.go | 3 ++- internal/ui/fieldeditor.go | 2 ++ internal/ui/main.go | 31 ++++++++++++++++++++----------- packet/fieldvalue.go | 2 ++ packet/packetmetadata.go | 1 + protocolctl/protocolctl.go | 15 ++++++++++----- protocolctl/protocolctl_test.go | 13 +++++++++---- 8 files changed, 48 insertions(+), 21 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index d9ef890..b48bd56 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -9,6 +9,7 @@ import ( var fyneApp fyne.App var w fyne.Window +// Appmain creates the Fyne application and starts it up func Appmain() { fyneApp = app.New() w = fyneApp.NewWindow("ProtocolPacketForger") @@ -19,6 +20,7 @@ func Appmain() { w.ShowAndRun() } +// CreateApp creates the PPF UI func CreateApp() *fyne.Container { ppf := ui.NewPPF(fyneApp, w) return ppf.GetContainer() diff --git a/internal/ui/fieldadd.go b/internal/ui/fieldadd.go index 1be9064..85d06b3 100644 --- a/internal/ui/fieldadd.go +++ b/internal/ui/fieldadd.go @@ -5,10 +5,11 @@ import ( "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 { fieldAdder := widget.NewButton("Add Field", func() { f := protocolctl.NewEmptyField() - index := protocolctl.AppendField(pEd.Reference, f) + index := protocolctl.AppendField(pEd.Reference, *f) pEd.AddFieldCreator(CreateFieldEditor(pEd, index, f)) }) return fieldAdder diff --git a/internal/ui/fieldeditor.go b/internal/ui/fieldeditor.go index 2d94600..247a37e 100644 --- a/internal/ui/fieldeditor.go +++ b/internal/ui/fieldeditor.go @@ -13,6 +13,7 @@ import ( "gitea.mmo.to/ppForge/ppforge/protocolctl" ) +// FieldEditor is a struct holding all elements of the field editor ui type FieldEditor struct { Representation *fyne.Container NameValue *widget.Entry @@ -29,6 +30,7 @@ type FieldEditor struct { 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 { fc := &FieldEditor{} fc.Reference = ref diff --git a/internal/ui/main.go b/internal/ui/main.go index 942ab70..b55e402 100644 --- a/internal/ui/main.go +++ b/internal/ui/main.go @@ -36,8 +36,8 @@ type TabProvider interface { Tab() *container.TabItem } -// ppfApp is the internal struct holding all the required main objects/elements for the application -type ppfApp struct { +// PpfApp is the internal struct holding all the required main objects/elements for the application +type PpfApp struct { App fyne.App Window fyne.Window // 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 -var PPF ppfApp +var PPF PpfApp // 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.Window = w PPF.Toolbar = CreateToolbar(fyneApp) @@ -81,7 +81,8 @@ func NewPPF(fyneApp fyne.App, w fyne.Window) ppfApp { 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( ppf.Toolbar, //top 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: // PROTOCOL editor // create a new protocol @@ -107,11 +109,13 @@ func (ppf *ppfApp) NewProtocolFile() { 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 { if v.Text == path { // do not open a file twice! @@ -128,11 +132,13 @@ func (ppf *ppfApp) OpenProtocolFile(path string) { 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() for _, fh := range ppf.OpenObjects { if fh.(TabProvider).Tab() == item { @@ -158,7 +164,8 @@ func (ppf *ppfApp) SaveFile(path string) { ppf.OpenTabs.Refresh() } -func (ppf *ppfApp) NewFile() { +// NewFile shows the new item dialog +func (ppf *PpfApp) NewFile() { for _, v := range ppf.OpenTabs.Items { if v.Text == "[new item dialog]" { ppf.OpenTabs.Select(v) @@ -170,6 +177,7 @@ func (ppf *ppfApp) NewFile() { ppf.OpenTabs.Select(item) } +// CreateToolbar initializes the toolbar func CreateToolbar(fyneApp fyne.App) fyne.CanvasObject { toolbar := widget.NewToolbar() toolbar.Append(widget.NewToolbarAction(theme.ContentAddIcon(), func() { @@ -207,6 +215,7 @@ func CreateToolbar(fyneApp fyne.App) fyne.CanvasObject { return toolbar } +// CreateWorkarea initializes the workarea func CreateWorkarea() *container.DocTabs { tabs := container.NewDocTabs() tabs.OnClosed = func(ti *container.TabItem) { diff --git a/packet/fieldvalue.go b/packet/fieldvalue.go index 3a8d6c9..b8384c9 100644 --- a/packet/fieldvalue.go +++ b/packet/fieldvalue.go @@ -9,10 +9,12 @@ type FieldValue struct { Value string } +// GetProtocolField returns the protocol field func (fv *FieldValue) GetProtocolField() *protocol.Field { return fv.fieldReference } +// SetProtocolField sets the protocl field func (fv *FieldValue) SetProtocolField(pf *protocol.Field) { fv.fieldReference = pf } diff --git a/packet/packetmetadata.go b/packet/packetmetadata.go index 7780be2..01b604e 100644 --- a/packet/packetmetadata.go +++ b/packet/packetmetadata.go @@ -1,5 +1,6 @@ package packet +// Metadata struct is the absolute minimum of metadata type Metadata struct { Name string } diff --git a/protocolctl/protocolctl.go b/protocolctl/protocolctl.go index f2b5acb..77f9adc 100644 --- a/protocolctl/protocolctl.go +++ b/protocolctl/protocolctl.go @@ -2,6 +2,7 @@ package protocolctl import ( "encoding/json" + "fmt" "io/fs" "os" @@ -56,14 +57,14 @@ func NewField( return &f } -func AppendField(prot *protocol.ProtocolStructure, field *protocol.Field) int { +func AppendField(prot *protocol.ProtocolStructure, field protocol.Field) int { i := len(prot.Structure) - prot.Structure = append(prot.Structure, field) + prot.Structure = append(prot.Structure, &field) return i + 1 } func AddField(prot *protocol.ProtocolStructure, index int, field *protocol.Field) { if len(prot.Structure) == index { - AppendField(prot, field) + AppendField(prot, *field) return } ret := make([]*protocol.Field, 0) @@ -104,8 +105,12 @@ func RemoveFieldByName(prot *protocol.ProtocolStructure, name string) { func RemoveFieldByElement(prot *protocol.ProtocolStructure, field int) { ret := make([]*protocol.Field, 0) - ret = append(ret, prot.Structure[:field]...) - ret = append(ret, prot.Structure[field+1:]...) + 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 } diff --git a/protocolctl/protocolctl_test.go b/protocolctl/protocolctl_test.go index 0b440ef..db36f13 100644 --- a/protocolctl/protocolctl_test.go +++ b/protocolctl/protocolctl_test.go @@ -43,7 +43,7 @@ func TestUpdateMetaData(t *testing.T) { func GenerateFields() []protocol.Field { slc := make([]protocol.Field, 0) for i := 0; i < 10; i++ { - slc = append(slc, *NewField( + field := NewField( fmt.Sprintf("testfield%d", i), fmt.Sprintf("Description %d", i), "", @@ -51,7 +51,8 @@ func GenerateFields() []protocol.Field { nil, false, false, - )) + ) + slc = append(slc, *field) } return slc } @@ -60,9 +61,13 @@ func GenerateStructure() *protocol.ProtocolStructure { p := NewProtocolStructure() slc := GenerateFields() for i, e := range slc { - AppendField(p, &e) + AppendField(p, e) 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 } @@ -77,7 +82,7 @@ func TestFieldAppend(t *testing.T) { false, ) p := NewProtocolStructure() - AppendField(p, f) + AppendField(p, *f) if p.Structure[0].Name != "testfield" { t.Log("Append failed.") t.Fail()