(impl any)
| 73 | } |
| 74 | |
| 75 | func 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)) |
no test coverage detected