Skip to content

Commit

Permalink
fix unmarshaling of action items
Browse files Browse the repository at this point in the history
  • Loading branch information
pomdtr committed Sep 19, 2024
1 parent df6ca99 commit b1b727a
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions pkg/sunbeam/action.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,51 @@
package sunbeam

import (
"encoding/json"
"fmt"
)

type ActionItem struct {
Title string `json:"title,omitempty"`
Key string `json:"key,omitempty"`
Type ActionType `json:"type,omitempty"`

Copy CopyAction `json:"copy,omitempty"`
Run RunAction `json:"run,omitempty"`
Open OpenAction `json:"open,omitempty"`
Reload ReloadAction `json:"reload,omitempty"`
Copy CopyAction `json:"-"`
Run RunAction `json:"-"`
Open OpenAction `json:"-"`
Reload ReloadAction `json:"-"`
}

func (a *ActionItem) UnmarshalJSON(data []byte) error {
var aux struct {
Type ActionType `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Key string `json:"key,omitempty"`
}

if err := json.Unmarshal(data, &aux); err != nil {
return err
}

a.Title = aux.Title
a.Type = aux.Type
a.Key = aux.Key

switch aux.Type {
case ActionTypeCopy:
return json.Unmarshal(data, &a.Copy)
case ActionTypeRun:
return json.Unmarshal(data, &a.Run)
case ActionTypeOpen:
return json.Unmarshal(data, &a.Open)
case ActionTypeReload:
return json.Unmarshal(data, &a.Reload)
case ActionTypeExit:
return nil
default:
return fmt.Errorf("unsupported page type: %s", aux.Type)
}

}

type ReloadAction struct {
Expand Down

0 comments on commit b1b727a

Please sign in to comment.