update BlockService to use the new type union feature of tsgen. strongly type the arguments to BlockService.SendCommand

This commit is contained in:
sawka
2024-06-12 13:47:13 -07:00
parent 605b9ea048
commit 083e00227e
13 changed files with 195 additions and 101 deletions
+35 -17
View File
@@ -9,6 +9,7 @@ import (
"reflect"
"github.com/wavetermdev/thenextwave/pkg/shellexec"
"github.com/wavetermdev/thenextwave/pkg/tsgen/tsgenmeta"
)
const CommandKey = "command"
@@ -21,16 +22,33 @@ const (
)
var CommandToTypeMap = map[string]reflect.Type{
BlockCommand_Message: reflect.TypeOf(MessageCommand{}),
BlockCommand_Input: reflect.TypeOf(InputCommand{}),
BlockCommand_SetView: reflect.TypeOf(SetViewCommand{}),
BlockCommand_SetMeta: reflect.TypeOf(SetMetaCommand{}),
BlockCommand_Message: reflect.TypeOf(BlockMessageCommand{}),
BlockCommand_Input: reflect.TypeOf(BlockInputCommand{}),
BlockCommand_SetView: reflect.TypeOf(BlockSetViewCommand{}),
BlockCommand_SetMeta: reflect.TypeOf(BlockSetMetaCommand{}),
}
func CommandTypeUnionMeta() tsgenmeta.TypeUnionMeta {
return tsgenmeta.TypeUnionMeta{
BaseType: reflect.TypeOf((*BlockCommand)(nil)).Elem(),
TypeFieldName: "command",
Types: []reflect.Type{
reflect.TypeOf(BlockMessageCommand{}),
reflect.TypeOf(BlockInputCommand{}),
reflect.TypeOf(BlockSetViewCommand{}),
reflect.TypeOf(BlockSetMetaCommand{}),
},
}
}
type BlockCommand interface {
GetCommand() string
}
type BlockCommandWrapper struct {
BlockCommand
}
func ParseCmdMap(cmdMap map[string]any) (BlockCommand, error) {
cmdType, ok := cmdMap[CommandKey].(string)
if !ok {
@@ -52,40 +70,40 @@ func ParseCmdMap(cmdMap map[string]any) (BlockCommand, error) {
return cmd.(BlockCommand), nil
}
type MessageCommand struct {
Command string `json:"command"`
type BlockMessageCommand struct {
Command string `json:"command" tstype:"\"message\""`
Message string `json:"message"`
}
func (mc *MessageCommand) GetCommand() string {
func (mc *BlockMessageCommand) GetCommand() string {
return BlockCommand_Message
}
type InputCommand struct {
Command string `json:"command"`
InputData64 string `json:"inputdata64"`
type BlockInputCommand struct {
Command string `json:"command" tstype:"\"controller:input\""`
InputData64 string `json:"inputdata64,omitempty"`
SigName string `json:"signame,omitempty"`
TermSize *shellexec.TermSize `json:"termsize,omitempty"`
}
func (ic *InputCommand) GetCommand() string {
func (ic *BlockInputCommand) GetCommand() string {
return BlockCommand_Input
}
type SetViewCommand struct {
Command string `json:"command"`
type BlockSetViewCommand struct {
Command string `json:"command" tstype:"\"setview\""`
View string `json:"view"`
}
func (svc *SetViewCommand) GetCommand() string {
func (svc *BlockSetViewCommand) GetCommand() string {
return BlockCommand_SetView
}
type SetMetaCommand struct {
Command string `json:"command"`
type BlockSetMetaCommand struct {
Command string `json:"command" tstype:"\"setmeta\""`
Meta map[string]any `json:"meta"`
}
func (smc *SetMetaCommand) GetCommand() string {
func (smc *BlockSetMetaCommand) GetCommand() string {
return BlockCommand_SetMeta
}
+6 -6
View File
@@ -40,7 +40,7 @@ type BlockController struct {
Status string
ShellProc *shellexec.ShellProc
ShellInputCh chan *InputCommand
ShellInputCh chan *BlockInputCommand
}
func (bc *BlockController) WithLock(f func()) {
@@ -149,7 +149,7 @@ func (bc *BlockController) DoRunShellCommand(rc *RunShellOpts) error {
bc.ShellProc.Close()
return err
}
shellInputCh := make(chan *InputCommand)
shellInputCh := make(chan *BlockInputCommand)
bc.ShellInputCh = shellInputCh
go func() {
defer func() {
@@ -234,7 +234,7 @@ func (bc *BlockController) Run(bdata *wstore.Block) {
for genCmd := range bc.InputCh {
switch cmd := genCmd.(type) {
case *InputCommand:
case *BlockInputCommand:
fmt.Printf("INPUT: %s | %q\n", bc.BlockId, cmd.InputData64)
if bc.ShellInputCh != nil {
bc.ShellInputCh <- cmd
@@ -293,10 +293,10 @@ func ProcessStaticCommand(blockId string, cmdGen BlockCommand) error {
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
defer cancelFn()
switch cmd := cmdGen.(type) {
case *MessageCommand:
case *BlockMessageCommand:
log.Printf("MESSAGE: %s | %q\n", blockId, cmd.Message)
return nil
case *SetViewCommand:
case *BlockSetViewCommand:
log.Printf("SETVIEW: %s | %q\n", blockId, cmd.View)
block, err := wstore.DBGet[*wstore.Block](ctx, blockId)
if err != nil {
@@ -308,7 +308,7 @@ func ProcessStaticCommand(blockId string, cmdGen BlockCommand) error {
return fmt.Errorf("error updating block: %w", err)
}
return nil
case *SetMetaCommand:
case *BlockSetMetaCommand:
log.Printf("SETMETA: %s | %v\n", blockId, cmd.Meta)
block, err := wstore.DBGet[*wstore.Block](ctx, blockId)
if err != nil {