MCPcopy
hub / github.com/wavetermdev/waveterm / serverImplAdapter

Function serverImplAdapter

pkg/wshutil/wshadapter.go:75–177  ·  view source on GitHub ↗
(impl any)

Source from the content-addressed store, hash-verified

73}
74
75func serverImplAdapter(impl any) func(*RpcResponseHandler) bool {
76 if impl == nil {
77 return noImplHandler
78 }
79 rtype := reflect.TypeOf(impl)
80 if rtype.Kind() != reflect.Ptr && rtype.Elem().Kind() != reflect.Struct {
81 panic(fmt.Sprintf("expected struct pointer, got %s", rtype))
82 }
83 // returns isAsync
84 return func(handler *RpcResponseHandler) bool {
85 cmd := handler.GetCommand()
86 methodDecl := WshCommandDeclMap[cmd]
87 if methodDecl == nil {
88 handler.SendResponseError(fmt.Errorf("command %q not found", cmd))
89 return true
90 }
91 rmethod := findCmdMethod(impl, cmd)
92 if rmethod == nil {
93 if !handler.NeedsResponse() && cmd != wshrpc.Command_Message {
94 // we also send an out of band message here since this is likely unexpected and will require debugging
95 handler.SendMessage(fmt.Sprintf("command %q method %q not found", handler.GetCommand(), methodDecl.MethodName))
96 }
97 handler.SendResponseError(fmt.Errorf("command not implemented %q", cmd))
98 return true
99 }
100 implMethod := reflect.ValueOf(impl).MethodByName(rmethod.Name)
101 var callParams []reflect.Value
102 callParams = append(callParams, reflect.ValueOf(handler.Context()))
103 commandDataTypes := methodDecl.GetCommandDataTypes()
104 if len(commandDataTypes) == 1 {
105 cmdData, err := recodeCommandData(cmd, handler.GetCommandRawData(), commandDataTypes[0])
106 if err != nil {
107 handler.SendResponseError(err)
108 return true
109 }
110 callParams = append(callParams, reflect.ValueOf(cmdData))
111 } else if len(commandDataTypes) > 1 {
112 multiArgAny, err := recodeCommandData(cmd, handler.GetCommandRawData(), multiArgRType)
113 if err != nil {
114 handler.SendResponseError(err)
115 return true
116 }
117 multiArg, ok := multiArgAny.(wshrpc.MultiArg)
118 if !ok {
119 handler.SendResponseError(fmt.Errorf("command %q invalid multi arg payload", cmd))
120 return true
121 }
122 if len(multiArg.Args) != len(commandDataTypes) {
123 handler.SendResponseError(fmt.Errorf("command %q expected %d args, got %d", cmd, len(commandDataTypes), len(multiArg.Args)))
124 return true
125 }
126 for idx, commandDataType := range commandDataTypes {
127 cmdData, err := recodeCommandData(cmd, multiArg.Args[idx], commandDataType)
128 if err != nil {
129 handler.SendResponseError(err)
130 return true
131 }
132 callParams = append(callParams, reflect.ValueOf(cmdData))

Callers 1

handleRequestInternalMethod · 0.85

Calls 13

PanicHandlerFunction · 0.92
findCmdMethodFunction · 0.85
recodeCommandDataFunction · 0.85
decodeRtnValsFunction · 0.85
GetCommandMethod · 0.80
SendResponseErrorMethod · 0.80
NeedsResponseMethod · 0.80
SendMessageMethod · 0.80
GetCommandDataTypesMethod · 0.80
GetCommandRawDataMethod · 0.80
SendResponseMethod · 0.80
FinalizeMethod · 0.80

Tested by

no test coverage detected