-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathresolver.go
62 lines (56 loc) · 1.33 KB
/
resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package nkn
import (
"context"
"log"
"github.com/nknorg/nkngomobile"
)
type Resolver interface {
nkngomobile.Resolver
ResolveContext(ctx context.Context, address string) (string, error)
}
func ResolveDest(ctx context.Context, dest string, resolvers []Resolver) (string, bool, error) {
var err error
for _, r := range resolvers {
var d string
d, err = r.ResolveContext(ctx, dest)
if err != nil {
return "", true, err
}
if len(d) == 0 {
continue
}
return d, true, nil
}
return dest, false, err
}
func ResolveDestN(ctx context.Context, dest string, resolvers []Resolver, depth int32) (string, error) {
for i := int32(0); i < depth; i++ {
d, isResolved, err := ResolveDest(ctx, dest, resolvers)
if err != nil {
return "", err
}
if !isResolved {
return dest, nil
}
dest = d
}
return "", ErrResolveLimit
}
func ResolveDests(ctx context.Context, dests []string, resolvers []Resolver, depth int32) ([]string, error) {
if len(dests) == 0 {
return nil, nil
}
resolvedDests := make([]string, 0, len(dests))
for _, dest := range dests {
resolvedDest, err := ResolveDestN(ctx, dest, resolvers, depth)
if err != nil {
log.Println(err)
continue
}
resolvedDests = append(resolvedDests, resolvedDest)
}
if len(resolvedDests) == 0 {
return nil, ErrInvalidDestination
}
return resolvedDests, nil
}