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
+5 -9
View File
@@ -9,25 +9,21 @@ import (
"time"
"github.com/wavetermdev/thenextwave/pkg/blockcontroller"
"github.com/wavetermdev/thenextwave/pkg/service/servicemeta"
"github.com/wavetermdev/thenextwave/pkg/tsgen/tsgenmeta"
)
type BlockService struct{}
const DefaultTimeout = 2 * time.Second
func (bs *BlockService) SendCommand_Meta() servicemeta.MethodMeta {
return servicemeta.MethodMeta{
func (bs *BlockService) SendCommand_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
Desc: "send command to block",
ArgNames: []string{"blockid", "command"},
ArgNames: []string{"blockid", "cmd"},
}
}
func (bs *BlockService) SendCommand(blockId string, cmdMap map[string]any) error {
cmd, err := blockcontroller.ParseCmdMap(cmdMap)
if err != nil {
return fmt.Errorf("error parsing command map: %w", err)
}
func (bs *BlockService) SendCommand(blockId string, cmd blockcontroller.BlockCommand) error {
if strings.HasPrefix(cmd.GetCommand(), "controller:") {
bc := blockcontroller.GetBlockController(blockId)
if bc == nil {
+19 -19
View File
@@ -12,7 +12,7 @@ import (
"time"
"github.com/wavetermdev/thenextwave/pkg/blockcontroller"
"github.com/wavetermdev/thenextwave/pkg/service/servicemeta"
"github.com/wavetermdev/thenextwave/pkg/tsgen/tsgenmeta"
"github.com/wavetermdev/thenextwave/pkg/waveobj"
"github.com/wavetermdev/thenextwave/pkg/wstore"
)
@@ -29,8 +29,8 @@ func parseORef(oref string) (*waveobj.ORef, error) {
return &waveobj.ORef{OType: fields[0], OID: fields[1]}, nil
}
func (svc *ObjectService) GetObject_Meta() servicemeta.MethodMeta {
return servicemeta.MethodMeta{
func (svc *ObjectService) GetObject_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
Desc: "get wave object by oref",
ArgNames: []string{"oref"},
}
@@ -50,8 +50,8 @@ func (svc *ObjectService) GetObject(orefStr string) (waveobj.WaveObj, error) {
return obj, nil
}
func (svc *ObjectService) GetObjects_Meta() servicemeta.MethodMeta {
return servicemeta.MethodMeta{
func (svc *ObjectService) GetObjects_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
ArgNames: []string{"orefs"},
ReturnDesc: "objects",
}
@@ -92,8 +92,8 @@ func updatesRtn(ctx context.Context, rtnVal map[string]any) (any, error) {
return rtnVal, nil
}
func (svc *ObjectService) AddTabToWorkspace_Meta() servicemeta.MethodMeta {
return servicemeta.MethodMeta{
func (svc *ObjectService) AddTabToWorkspace_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
ArgNames: []string{"uiContext", "tabName", "activateTab"},
ReturnDesc: "tabId",
}
@@ -120,8 +120,8 @@ func (svc *ObjectService) AddTabToWorkspace(uiContext wstore.UIContext, tabName
return tab.OID, wstore.ContextGetUpdatesRtn(ctx), nil
}
func (svc *ObjectService) SetActiveTab_Meta() servicemeta.MethodMeta {
return servicemeta.MethodMeta{
func (svc *ObjectService) SetActiveTab_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
ArgNames: []string{"uiContext", "tabId"},
}
}
@@ -158,8 +158,8 @@ func (svc *ObjectService) SetActiveTab(uiContext wstore.UIContext, tabId string)
return updates, nil
}
func (svc *ObjectService) CreateBlock_Meta() servicemeta.MethodMeta {
return servicemeta.MethodMeta{
func (svc *ObjectService) CreateBlock_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
ArgNames: []string{"uiContext", "blockDef", "rtOpts"},
ReturnDesc: "blockId",
}
@@ -185,8 +185,8 @@ func (svc *ObjectService) CreateBlock(uiContext wstore.UIContext, blockDef *wsto
return blockData.OID, wstore.ContextGetUpdatesRtn(ctx), nil
}
func (svc *ObjectService) DeleteBlock_Meta() servicemeta.MethodMeta {
return servicemeta.MethodMeta{
func (svc *ObjectService) DeleteBlock_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
ArgNames: []string{"uiContext", "blockId"},
}
}
@@ -203,8 +203,8 @@ func (svc *ObjectService) DeleteBlock(uiContext wstore.UIContext, blockId string
return wstore.ContextGetUpdatesRtn(ctx), nil
}
func (svc *ObjectService) CloseTab_Meta() servicemeta.MethodMeta {
return servicemeta.MethodMeta{
func (svc *ObjectService) CloseTab_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
ArgNames: []string{"uiContext", "tabId"},
}
}
@@ -244,8 +244,8 @@ func (svc *ObjectService) CloseTab(uiContext wstore.UIContext, tabId string) (ws
return wstore.ContextGetUpdatesRtn(ctx), nil
}
func (svc *ObjectService) UpdateObjectMeta_Meta() servicemeta.MethodMeta {
return servicemeta.MethodMeta{
func (svc *ObjectService) UpdateObjectMeta_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
ArgNames: []string{"uiContext", "oref", "meta"},
}
}
@@ -265,8 +265,8 @@ func (svc *ObjectService) UpdateObjectMeta(uiContext wstore.UIContext, orefStr s
return wstore.ContextGetUpdatesRtn(ctx), nil
}
func (svc *ObjectService) UpdateObject_Meta() servicemeta.MethodMeta {
return servicemeta.MethodMeta{
func (svc *ObjectService) UpdateObject_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
ArgNames: []string{"uiContext", "waveObj", "returnUpdates"},
}
}
+19 -4
View File
@@ -9,11 +9,12 @@ import (
"reflect"
"strings"
"github.com/wavetermdev/thenextwave/pkg/blockcontroller"
"github.com/wavetermdev/thenextwave/pkg/service/blockservice"
"github.com/wavetermdev/thenextwave/pkg/service/clientservice"
"github.com/wavetermdev/thenextwave/pkg/service/fileservice"
"github.com/wavetermdev/thenextwave/pkg/service/objectservice"
"github.com/wavetermdev/thenextwave/pkg/service/servicemeta"
"github.com/wavetermdev/thenextwave/pkg/tsgen/tsgenmeta"
"github.com/wavetermdev/thenextwave/pkg/waveobj"
"github.com/wavetermdev/thenextwave/pkg/wstore"
)
@@ -31,9 +32,10 @@ var updatesRType = reflect.TypeOf(([]wstore.WaveObjUpdate{}))
var waveObjRType = reflect.TypeOf((*waveobj.WaveObj)(nil)).Elem()
var waveObjSliceRType = reflect.TypeOf([]waveobj.WaveObj{})
var waveObjMapRType = reflect.TypeOf(map[string]waveobj.WaveObj{})
var methodMetaRType = reflect.TypeOf(servicemeta.MethodMeta{})
var methodMetaRType = reflect.TypeOf(tsgenmeta.MethodMeta{})
var waveObjUpdateRType = reflect.TypeOf(wstore.WaveObjUpdate{})
var uiContextRType = reflect.TypeOf((*wstore.UIContext)(nil)).Elem()
var blockCommandRType = reflect.TypeOf((*blockcontroller.BlockCommand)(nil)).Elem()
type WebCallType struct {
Service string `json:"service"`
@@ -89,12 +91,25 @@ func convertComplex(argType reflect.Type, jsonArg any) (any, error) {
}
func isSpecialWaveArgType(argType reflect.Type) bool {
return argType == waveObjRType || argType == waveObjSliceRType || argType == waveObjMapRType
return argType == waveObjRType || argType == waveObjSliceRType || argType == waveObjMapRType || argType == blockCommandRType
}
func convertBlockCommand(argType reflect.Type, jsonArg any) (any, error) {
if _, ok := jsonArg.(map[string]any); !ok {
return nil, fmt.Errorf("cannot convert %T to %s", jsonArg, argType)
}
cmd, err := blockcontroller.ParseCmdMap(jsonArg.(map[string]any))
if err != nil {
return nil, fmt.Errorf("error parsing command map: %w", err)
}
return cmd, nil
}
func convertSpecial(argType reflect.Type, jsonArg any) (any, error) {
jsonType := reflect.TypeOf(jsonArg)
if argType == waveObjRType {
if argType == blockCommandRType {
return convertBlockCommand(argType, jsonArg)
} else if argType == waveObjRType {
if jsonType.Kind() != reflect.Map {
return nil, fmt.Errorf("cannot convert %T to %s", jsonArg, argType)
}
-10
View File
@@ -1,10 +0,0 @@
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package servicemeta
type MethodMeta struct {
Desc string
ArgNames []string
ReturnDesc string
}