/
validfield.go
58 lines (52 loc) · 1.33 KB
/
validfield.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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package utils
import (
"strings"
"github.com/juju/errors"
"github.com/juju/mgo/v3/bson"
)
// IsValidFieldName returns true if the given name is acceptable for
// use as a MongoDB field name.
func IsValidFieldName(name string) bool {
if len(name) == 0 {
return false
}
if strings.HasPrefix(name, "$") {
return false
}
if strings.Contains(name, ".") {
return false
}
return true
}
// CheckStorable returns an error if the given document - or any of
// it's subdocuments - contains field names which are not valid for
// storage into MongoDB.
func CheckStorable(inDoc interface{}) error {
// Normalise document to bson.M
bytes, err := bson.Marshal(inDoc)
if err != nil {
return errors.Annotate(err, "marshalling")
}
var doc bson.M
if err := bson.Unmarshal(bytes, &doc); err != nil {
return errors.Annotate(err, "unmarshalling")
}
// Check it.
return errors.Trace(checkDoc(doc))
}
func checkDoc(doc bson.M) error {
for name, value := range doc {
if !IsValidFieldName(name) {
return errors.Errorf("%q is not a valid field name", name)
}
// If the field is a subdocument, recurse into it.
if subDoc, ok := value.(bson.M); ok {
if err := checkDoc(subDoc); err != nil {
return errors.Trace(err)
}
}
}
return nil
}