Skip to content

Commit

Permalink
fixup! fixup! wip: maybe progress?
Browse files Browse the repository at this point in the history
  • Loading branch information
ja-he committed Jun 25, 2024
1 parent 518ba70 commit 444a328
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions internal/control/edit/editors/composite_editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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())
}
Expand All @@ -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")
}
Expand Down

0 comments on commit 444a328

Please sign in to comment.