NewCmdAndIDList takes a mix of CmdWithIDs and Cmds with an ID property and returns a CmdAndIDList. Cmds that have an ID property are transformed into CmdAndID by using the ID.
(cmds ...interface{})
| 63 | // returns a CmdAndIDList. |
| 64 | // Cmds that have an ID property are transformed into CmdAndID by using the ID. |
| 65 | func NewCmdAndIDList(cmds ...interface{}) CmdAndIDList { |
| 66 | l := CmdAndIDList{} |
| 67 | for _, a := range cmds { |
| 68 | switch a := a.(type) { |
| 69 | case api.Cmd: |
| 70 | p, err := api.GetParameter(a, "ID") |
| 71 | if err != nil { |
| 72 | panic(fmt.Errorf("Command %v does not have ID property: %v", a.CmdName(), err)) |
| 73 | } |
| 74 | v := reflect.ValueOf(p) |
| 75 | if v.Kind() != reflect.Uint64 { |
| 76 | panic(fmt.Errorf("Command %v has unexpected ID property type: %T", a.CmdName(), p)) |
| 77 | } |
| 78 | l = append(l, CmdAndID{a, api.CmdID(v.Uint())}) |
| 79 | case CmdAndID: |
| 80 | l = append(l, a) |
| 81 | default: |
| 82 | panic(fmt.Errorf("list only accepts types CmdWithID or CmdAndID. Got %T", a)) |
| 83 | } |
| 84 | } |
| 85 | return l |
| 86 | } |
| 87 | |
| 88 | func (l *CmdAndIDList) Write(ctx context.Context, id api.CmdID, c api.Cmd) { |
| 89 | *l = append(*l, CmdAndID{c, id}) |