@cryptotaxi247 / kubo / commits / 51c6a1c55

refactor(resolve) separate argument marshalling from the operation

@mappum see how the unpacking of arguments happens separately from the resolve loop? It's a bit more verbose, but much clearer. However, doing two different things in one loop is less clear than doing them separately. It also causes problems for further refactoring as it introduces temps that get in the way of further refactorings. Plus, there will be 50+ commands, so it's important that we stay framework agnostic as much as possible. So, this is the style we prefer. It'll keep us nimble in the long run.

Brian Tiger Chow committed Nov 5, 2014 at 01:34 UTC 51c6a1c5529d7ba8d625c649a20becd7e6492475
1 file changed +26 -36
core/commands2/name.go
+26 -36
@@ -82,47 +82,34 @@ var resolveCmd = &cmds.Command{
82 cmds.Argument{"name", cmds.ArgString, false, true},
83 },
84 Run: func(res cmds.Response, req cmds.Request) {
85 - name := ""
86 - args := req.Arguments()
85 +
86 n := req.Context().Node
88 - var output []IpnsEntry
87 + var names []string
88
90 - if len(args) == 0 {
89 + if len(req.Arguments()) == 0 {
90 if n.Identity == nil {
91 res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
92 return
93 }
95 -
96 - name = n.Identity.ID().String()
97 - entry, err := resolve(name, n)
98 - if err != nil {
99 - res.SetError(err, cmds.ErrNormal)
100 - return
101 - }
102 -
103 - output = []IpnsEntry{entry}
104 -
94 + names = append(names, n.Identity.ID().String())
95 } else {
106 - output = make([]IpnsEntry, len(args))
107 -
108 - for i, arg := range args {
109 - var ok bool
110 - name, ok = arg.(string)
96 + for _, arg := range req.Arguments() {
97 + name, ok := arg.(string)
98 if !ok {
99 res.SetError(errors.New("cast error"), cmds.ErrNormal)
100 return
101 }
115 -
116 - entry, err := resolve(name, n)
117 - if err != nil {
118 - res.SetError(err, cmds.ErrNormal)
119 - return
120 - }
121 - output[i] = entry
102 + names = append(names, name)
103 }
104 }
105
125 - res.SetOutput(&ResolveOutput{output})
106 + entries, err := resolve(n, names)
107 + if err != nil {
108 + res.SetError(err, cmds.ErrNormal)
109 + return
110 + }
111 +
112 + res.SetOutput(&ResolveOutput{entries})
113 },
114 Type: &ResolveOutput{},
115 }
@@ -145,14 +132,17 @@ func publish(n *core.IpfsNode, k crypto.PrivKey, ref string) (*IpnsEntry, error)
132 }, nil
133 }
134
148 -func resolve(name string, n *core.IpfsNode) (IpnsEntry, error) {
149 - resolved, err := n.Namesys.Resolve(name)
150 - if err != nil {
151 - return IpnsEntry{}, err
135 +func resolve(n *core.IpfsNode, names []string) ([]IpnsEntry, error) {
136 + var entries []IpnsEntry
137 + for _, name := range names {
138 + resolved, err := n.Namesys.Resolve(name)
139 + if err != nil {
140 + return nil, err
141 + }
142 + entries = append(entries, IpnsEntry{
143 + Name: name,
144 + Value: resolved,
145 + })
146 }
153 -
154 - return IpnsEntry{
155 - Name: name,
156 - Value: resolved,
157 - }, nil
147 + return entries, nil
148 }