forked from tesspib/protobson
/
namer.go
96 lines (80 loc) · 3.28 KB
/
namer.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package protobson
import (
"fmt"
"google.golang.org/protobuf/reflect/protoreflect"
"strconv"
"strings"
)
const (
fieldPrefix = "pb_field_"
)
// FieldNamer is used to build field name in document from its field descriptor
type FieldNamer interface {
// FieldDescriptorToFieldName is function to get document field name from proto field descriptor
FieldDescriptorToFieldName(fd protoreflect.FieldDescriptor) string
// FieldNameToFieldDescriptor is function to get proto field descriptor from document field name
FieldNameToFieldDescriptor(fd protoreflect.FieldDescriptors, name string) (protoreflect.FieldDescriptor, error)
}
// WithFieldNamer - returns options with FieldNamer instance
func WithFieldNamer(namer FieldNamer) Option {
return func(codec *protobufCodec) {
codec.fieldNamer = namer
}
}
// WithFieldNamerByNumber makes FieldNamer Option with FieldNamerByNumber instance
func WithFieldNamerByNumber() Option {
return WithFieldNamer(&FieldNamerByNumber{})
}
// WithFieldNamerByName makes FieldNamer Option with FieldNamerByName instance
func WithFieldNamerByName() Option {
return WithFieldNamer(&FieldNamerByName{})
}
// WithFieldNamerByJsonName makes FieldNamer Option with FieldNamerByJsonName instance
func WithFieldNamerByJsonName() Option {
return WithFieldNamer(&FieldNamerByJsonName{})
}
// FieldNamerByNumber makes field names based on field tag number. With this implementation fields in proto spec can
// be renamed and documents still can be decoded without loss of any data
type FieldNamerByNumber struct {
}
// FieldDescriptorToFieldName is implementation of FieldNamer method
func (f *FieldNamerByNumber) FieldDescriptorToFieldName(fd protoreflect.FieldDescriptor) string {
return fmt.Sprintf("%v%v", fieldPrefix, fd.Number())
}
// FieldNameToFieldDescriptor is implementation of FieldNamer method
func (f *FieldNamerByNumber) FieldNameToFieldDescriptor(fd protoreflect.FieldDescriptors,
name string) (protoreflect.FieldDescriptor, error) {
if !strings.HasPrefix(name, fieldPrefix) {
return nil, nil
}
numString := strings.TrimPrefix(name, fieldPrefix)
num, err := strconv.Atoi(numString)
if err != nil {
return nil, err
}
return fd.ByNumber(protoreflect.FieldNumber(num)), nil
}
// FieldNamerByName makes field name based on field name in proto spec
type FieldNamerByName struct {
}
// FieldDescriptorToFieldName is implementation of FieldNamer method
func (f *FieldNamerByName) FieldDescriptorToFieldName(fd protoreflect.FieldDescriptor) string {
return string(fd.Name())
}
// FieldNameToFieldDescriptor is implementation of FieldNamer method
func (f *FieldNamerByName) FieldNameToFieldDescriptor(fd protoreflect.FieldDescriptors,
name string) (protoreflect.FieldDescriptor, error) {
return fd.ByName(protoreflect.Name(name)), nil
}
// FieldNamerByJsonName makes field name based on json name of field
type FieldNamerByJsonName struct {
}
// FieldDescriptorToFieldName is implementation of FieldNamer method
func (f *FieldNamerByJsonName) FieldDescriptorToFieldName(fd protoreflect.FieldDescriptor) string {
return fd.JSONName()
}
// FieldNameToFieldDescriptor is implementation of FieldNamer method
func (f *FieldNamerByJsonName) FieldNameToFieldDescriptor(fd protoreflect.FieldDescriptors,
name string) (protoreflect.FieldDescriptor, error) {
return fd.ByJSONName(name), nil
}