/
destination.go
79 lines (70 loc) · 2.72 KB
/
destination.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package topoproto
import (
"encoding/hex"
"strings"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/vterrors"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
)
// ParseDestination parses the string representation of a Destination
// of the form keyspace:shard@tablet_type. You can use a / instead of a :.
func ParseDestination(targetString string, defaultTabletType topodatapb.TabletType) (string, topodatapb.TabletType, key.Destination, error) {
var dest key.Destination
var keyspace string
tabletType := defaultTabletType
last := strings.LastIndexAny(targetString, "@")
if last != -1 {
// No need to check the error. UNKNOWN will be returned on
// error and it will fail downstream.
tabletType, _ = ParseTabletType(targetString[last+1:])
targetString = targetString[:last]
}
last = strings.LastIndexAny(targetString, "/:")
if last != -1 {
dest = key.DestinationShard(targetString[last+1:])
targetString = targetString[:last]
}
// Try to parse it as a keyspace id or range
last = strings.LastIndexAny(targetString, "[")
if last != -1 {
rangeEnd := strings.LastIndexAny(targetString, "]")
if rangeEnd == -1 {
return keyspace, tabletType, dest, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "invalid key range provided. Couldn't find range end ']'")
}
rangeString := targetString[last+1 : rangeEnd]
if strings.Contains(rangeString, "-") {
// Parse as range
keyRange, err := key.ParseShardingSpec(rangeString)
if err != nil {
return keyspace, tabletType, dest, err
}
if len(keyRange) != 1 {
return keyspace, tabletType, dest, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "single keyrange expected in %s", rangeString)
}
dest = key.DestinationExactKeyRange{KeyRange: keyRange[0]}
} else {
// Parse as keyspace id
destBytes, err := hex.DecodeString(rangeString)
if err != nil {
return keyspace, tabletType, dest, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "expected valid hex in keyspace id %s", rangeString)
}
dest = key.DestinationKeyspaceID(destBytes)
}
targetString = targetString[:last]
}
keyspace = targetString
return keyspace, tabletType, dest, nil
}