(cfunc any)
| 122 | } |
| 123 | |
| 124 | func validateCFunc(cfunc any) error { |
| 125 | if cfunc == nil { |
| 126 | return fmt.Errorf("Component function cannot b nil") |
| 127 | } |
| 128 | rval := reflect.ValueOf(cfunc) |
| 129 | if rval.Kind() != reflect.Func { |
| 130 | return fmt.Errorf("Component function must be a function") |
| 131 | } |
| 132 | rtype := rval.Type() |
| 133 | if rtype.NumIn() != 2 { |
| 134 | return fmt.Errorf("Component function must take exactly 2 arguments") |
| 135 | } |
| 136 | if rtype.NumOut() != 1 { |
| 137 | return fmt.Errorf("Component function must return exactly 1 value") |
| 138 | } |
| 139 | // first arg must be context.Context |
| 140 | if rtype.In(0) != reflect.TypeOf((*context.Context)(nil)).Elem() { |
| 141 | return fmt.Errorf("Component function first argument must be context.Context") |
| 142 | } |
| 143 | // second can a map[string]any, or a struct, or ptr to struct (we'll reflect the value into it) |
| 144 | arg2Type := rtype.In(1) |
| 145 | if arg2Type.Kind() == reflect.Ptr { |
| 146 | arg2Type = arg2Type.Elem() |
| 147 | } |
| 148 | if arg2Type.Kind() == reflect.Map { |
| 149 | if arg2Type.Key().Kind() != reflect.String || |
| 150 | !(arg2Type.Elem().Kind() == reflect.Interface && arg2Type.Elem().NumMethod() == 0) { |
| 151 | return fmt.Errorf("Map argument must be map[string]any") |
| 152 | } |
| 153 | } else if arg2Type.Kind() != reflect.Struct && |
| 154 | !(arg2Type.Kind() == reflect.Interface && arg2Type.NumMethod() == 0) { |
| 155 | return fmt.Errorf("Component function second argument must be map[string]any, struct, or any") |
| 156 | } |
| 157 | return nil |
| 158 | } |
| 159 | |
| 160 | func (r *RootElem) RegisterComponent(name string, cfunc any) error { |
| 161 | if err := validateCFunc(cfunc); err != nil { |
no test coverage detected