-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdatastore_name.go
More file actions
83 lines (70 loc) · 2.48 KB
/
datastore_name.go
File metadata and controls
83 lines (70 loc) · 2.48 KB
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
80
81
82
83
// Copyright 2023-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package sgbucket
import (
"fmt"
"regexp"
)
// DataStoreName provides the methods that can give you each part of a data store.
//
// Each implementation is free to decide how to store the data store name, to avoid both sgbucket leaking into implementations,
// and also reduce duplication for storing these values, in the event SDKs already hold copies of names internally.
type DataStoreName interface {
ScopeName() string
CollectionName() string
}
// Simple struct implementation of DataStoreName.
type DataStoreNameImpl struct {
Scope, Collection string
}
const (
DefaultCollection = "_default" // Name of the default collection
DefaultScope = "_default" // Name of the default collection
MobileSystemScope = "_system"
MobileSystemCollection = "_mobile"
ScopeCollectionSeparator = "." // Delimiter between scope & collection names
)
var dsNameRegexp = regexp.MustCompile("^[a-zA-Z0-9-][a-zA-Z0-9%_-]{0,250}$")
func (sc DataStoreNameImpl) ScopeName() string {
return sc.Scope
}
func (sc DataStoreNameImpl) CollectionName() string {
return sc.Collection
}
func (sc DataStoreNameImpl) String() string {
return sc.Scope + ScopeCollectionSeparator + sc.Collection
}
func (sc DataStoreNameImpl) IsDefault() bool {
return sc.Scope == DefaultScope && sc.Collection == DefaultCollection
}
// Validates the names and creates new scope and collection pair
func NewValidDataStoreName(scope, collection string) (id DataStoreNameImpl, err error) {
if IsValidDataStoreName(scope, collection) {
id = DataStoreNameImpl{scope, collection}
} else {
err = fmt.Errorf("invalid scope/collection name '%s.%s'", scope, collection)
}
return
}
// Returns true if scope.coll is a valid data store name.
func IsValidDataStoreName(scope, coll string) bool {
if scope == DefaultScope {
return coll == DefaultCollection || dsNameRegexp.MatchString(coll)
}
if scope == MobileSystemScope {
return coll == MobileSystemCollection
}
if dsNameRegexp.MatchString(scope) {
return dsNameRegexp.MatchString(coll)
}
return false
}
var (
// Enforce interface conformance:
_ DataStoreName = &DataStoreNameImpl{"a", "b"}
)