/
enum.go
124 lines (101 loc) · 3.17 KB
/
enum.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2024 The protobuf-tools Authors
// SPDX-License-Identifier: Apache-2.0
package enum
import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
"github.com/protobuf-tools/protoc-gen-docs/protodesc"
)
// Descriptor describes an enum type.
type Descriptor struct {
desc *descriptorpb.EnumDescriptorProto
comment *protodesc.Comment
Values ValueDescriptors
}
// NewDescriptor returns the new named [*Descriptor].
func NewDescriptor(name string) *Descriptor {
return &Descriptor{
desc: &descriptorpb.EnumDescriptorProto{
Name: proto.String(name),
},
}
}
// GetName gets the enum name.
func (d *Descriptor) GetName() string {
return d.desc.GetName()
}
// GetValue gets the enum values.
func (d *Descriptor) GetValue() ValueDescriptors {
return d.Values
}
// GetOptions gets the enum options.
func (d *Descriptor) GetOptions() *descriptorpb.EnumOptions {
return d.desc.GetOptions()
}
// GetReservedRange gets the enum reserved range.
func (d *Descriptor) GetReservedRange() []*descriptorpb.EnumDescriptorProto_EnumReservedRange {
return d.desc.GetReservedRange()
}
// GetReservedName gets the enum reserved names.
func (d *Descriptor) GetReservedName() []string {
return d.desc.GetReservedName()
}
// GetComment gets the enum comment.
func (d *Descriptor) GetComment() *protodesc.Comment {
return d.comment
}
// SetLeadingComment sets the enum leading comment.
func (d *Descriptor) SetLeadingComment(fn, leading string) {
// TODO(zchee): normalize with fn
_ = fn
d.comment.Leading = leading
}
// SetTrailingComment sets the enum trailing comment.
func (d *Descriptor) SetTrailingComment(trailing string) {
// TODO(zchee): normalize
d.comment.Trailing = trailing
}
// SetDetachedComment sets the enum detached comments.
func (d *Descriptor) SetDetachedComment(detached []string) {
// TODO(zchee): normalize
d.comment.Detached = detached
}
// Descriptor returns the enum descriptor.
func (d *Descriptor) Descriptor() *descriptorpb.EnumDescriptorProto {
return d.desc
}
// ValueDescriptor describes a value within an enum.
type ValueDescriptor struct {
desc *descriptorpb.EnumValueDescriptorProto
}
// NewValueDescriptor returns the new named [*ValueDescriptor] with number.
func NewValueDescriptor(name string, number int32) *ValueDescriptor {
return &ValueDescriptor{
desc: &descriptorpb.EnumValueDescriptorProto{
Name: proto.String(name),
Number: proto.Int32(number),
},
}
}
// GetName gets the enum name.
func (d *ValueDescriptor) GetName() string {
return d.desc.GetName()
}
// SetDeprecated sets the deprecated itself.
func (d *ValueDescriptor) SetDeprecated() {
d.desc.Options.Deprecated = proto.Bool(true)
}
// Descriptor returns the enum value descriptor.
func (d *ValueDescriptor) Descriptor() *descriptorpb.EnumValueDescriptorProto {
return d.desc
}
// ValueDescriptors describes a slice of value within an enum.
type ValueDescriptors []*ValueDescriptor
// Descriptor returns the enum value descriptors.
func (s ValueDescriptors) Descriptor() []*descriptorpb.EnumValueDescriptorProto {
values := make([]*descriptorpb.EnumValueDescriptorProto, len(s))
for i, ev := range s {
values[i] = ev.Descriptor()
}
return values
}