ParseMethod parses a method name into its components. Format: action_type[_modifier][_transport] Examples: echo_string, stream_object_final_sse, broadcast_string_ws Returns error if the method name is invalid.
(method string)
| 89 | // Examples: echo_string, stream_object_final_sse, broadcast_string_ws |
| 90 | // Returns error if the method name is invalid. |
| 91 | func ParseMethod(method string) (MethodInfo, error) { |
| 92 | // Check for grpc_ prefix convention |
| 93 | grpc := false |
| 94 | if strings.HasPrefix(method, "grpc_") { |
| 95 | grpc = true |
| 96 | method = strings.TrimPrefix(method, "grpc_") |
| 97 | } |
| 98 | |
| 99 | parts := strings.Split(method, "_") |
| 100 | if len(parts) < 2 { |
| 101 | return MethodInfo{}, fmt.Errorf("invalid method name %q: must have format action_type[_modifier][_transport]", method) |
| 102 | } |
| 103 | |
| 104 | info := MethodInfo{ |
| 105 | Action: parts[0], |
| 106 | Type: parts[1], |
| 107 | GRPC: grpc, |
| 108 | } |
| 109 | |
| 110 | // Check if last part is a transport |
| 111 | if len(parts) > 2 { |
| 112 | lastPart := parts[len(parts)-1] |
| 113 | if lastPart == "sse" || lastPart == "ws" { |
| 114 | info.Transport = lastPart |
| 115 | parts = parts[:len(parts)-1] // Remove transport from parts |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Validate action |
| 120 | validActions := map[string]bool{ |
| 121 | ActionEcho: true, ActionTransform: true, ActionGenerate: true, |
| 122 | ActionStream: true, ActionCollect: true, ActionBroadcast: true, |
| 123 | } |
| 124 | if !validActions[info.Action] { |
| 125 | return MethodInfo{}, fmt.Errorf("invalid action %q in method %q: must be one of: %s", |
| 126 | info.Action, method, strings.Join(getMapKeys(validActions), ", ")) |
| 127 | } |
| 128 | |
| 129 | // Validate type |
| 130 | validTypes := map[string]bool{ |
| 131 | TypeString: true, TypeArray: true, TypeObject: true, |
| 132 | TypeMap: true, TypeUser: true, TypeInt: true, TypeBool: true, |
| 133 | } |
| 134 | if !validTypes[info.Type] { |
| 135 | return MethodInfo{}, fmt.Errorf("invalid type %q in method %q: must be one of: %s", |
| 136 | info.Type, method, strings.Join(getMapKeys(validTypes), ", ")) |
| 137 | } |
| 138 | |
| 139 | // Check for modifier (3rd part after action and type) |
| 140 | if len(parts) >= 3 { |
| 141 | info.Modifier = parts[2] |
| 142 | // Validate modifier |
| 143 | validModifiers := map[string]bool{ |
| 144 | ModifierNotify: true, ModifierError: true, ModifierValidate: true, ModifierFinal: true, ModifierIDMap: true, |
| 145 | } |
| 146 | if !validModifiers[info.Modifier] { |
| 147 | return MethodInfo{}, fmt.Errorf("invalid modifier %q in method %q: must be one of: %s", |
| 148 | info.Modifier, method, strings.Join(getMapKeys(validModifiers), ", ")) |