diff --git a/internal/control/edit/editors/composite_editor.go b/internal/control/edit/editors/composite_editor.go index b3260f12..269b4cf5 100644 --- a/internal/control/edit/editors/composite_editor.go +++ b/internal/control/edit/editors/composite_editor.go @@ -12,7 +12,6 @@ import ( "github.com/ja-he/dayplan/internal/control/edit" "github.com/ja-he/dayplan/internal/input" "github.com/ja-he/dayplan/internal/input/processors" - "github.com/ja-he/dayplan/internal/model" ) // An EditorID is a unique identifier for an editor within a composite editor. @@ -75,7 +74,7 @@ func (e *Composite) EnterField() { } // ConstructEditor constructs a new editor... -func ConstructEditor[T any](id string, obj *T, extraSpec map[string]any, parentEditor *Composite) (edit.Editor, error) { +func ConstructEditor(id string, obj any, extraSpec map[string]any, parentEditor *Composite) (edit.Editor, error) { structPtr := reflect.ValueOf(obj) if structPtr.Kind() != reflect.Ptr { @@ -120,7 +119,6 @@ func ConstructEditor[T any](id string, obj *T, extraSpec map[string]any, parentE } // go through all tags - activeFieldIDPopulated := false for i := 0; i < structValue.NumField(); i++ { field := structType.Field(i) @@ -149,9 +147,8 @@ func ConstructEditor[T any](id string, obj *T, extraSpec map[string]any, parentE // add the corresponding data to e (if not ignored) if !editspec.Ignore { // set the active field to the first field - if !activeFieldIDPopulated { + if constructedCompositeEditor.activeFieldID == "___unassigned" { constructedCompositeEditor.activeFieldID = subeditorID - activeFieldIDPopulated = true } constructedCompositeEditor.fieldOrder = append(constructedCompositeEditor.fieldOrder, subeditorID) @@ -195,12 +192,14 @@ func ConstructEditor[T any](id string, obj *T, extraSpec map[string]any, parentE } else { // construct the sub-editor for the struct f := structValue.Field(i) - typedSubfield, ok := f.Addr().Interface().(*model.Category) // TODO: no clue what i was smoking here... - if !ok { - return nil, fmt.Errorf("unable to cast field '%s' of type '%s' to model.Category", field.Name, field.Type.String()) + var fAsPtr any + if f.Kind() == reflect.Ptr { + fAsPtr = f.Interface() + } else { + fAsPtr = f.Addr().Interface() } log.Debug().Msgf("constructing subeditor for field '%s' of type '%s'", field.Name, field.Type.String()) - sube, err := ConstructEditor(field.Name, typedSubfield, nil, constructedCompositeEditor) + sube, err := ConstructEditor(field.Name, fAsPtr, nil, constructedCompositeEditor) if err != nil { return nil, fmt.Errorf("unable to construct subeditor for field '%s' of type '%s' (%s)", field.Name, field.Type.String(), err.Error()) } @@ -220,6 +219,9 @@ func ConstructEditor[T any](id string, obj *T, extraSpec map[string]any, parentE } + if len(constructedCompositeEditor.fieldOrder) == 0 { + return nil, fmt.Errorf("could not find any fields to edit") + } if constructedCompositeEditor.activeFieldID == "___unassigned" { return nil, fmt.Errorf("could not find a field to set as active") }