This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
/
file.go
88 lines (72 loc) · 1.66 KB
/
file.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
package registry
import (
"github.com/doozer-de/restgen/pbmap"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
)
const OptionDartPackage = "dart_package"
type Files []*File
func (fs *Files) Add(f *File) {
for _, c := range *fs {
if c.Name == f.Name {
return
}
}
*fs = append(*fs, f)
}
func (fs *Files) Get(key string) (*File, bool) {
for _, c := range *fs {
if c.Name == key {
return c, true
}
}
return nil, false
}
type File struct {
Type *descriptorpb.FileDescriptorProto
Name string
Messages []*Message
Enums []*Enum
Package string
Registry *Registry
// Options holds additional options. For example our dart_package options
Options map[string]string
}
func NewFile(f *descriptorpb.FileDescriptorProto, r *Registry) *File {
file := &File{
Type: f,
Name: *f.Name,
Package: f.GetPackage(),
Registry: r,
Options: getAdditionalOptions(f),
}
for j, m := range f.MessageType {
file.Messages = append(file.Messages, NewMessage(m, file, j))
}
for j, e := range f.EnumType {
file.Enums = append(file.Enums, NewEnum(e, file, j))
}
return file
}
func getAdditionalOptions(f *descriptorpb.FileDescriptorProto) map[string]string {
m := make(map[string]string)
if dp, ok := dartPackageOption(f); ok {
m[OptionDartPackage] = dp
}
return m
}
func dartPackageOption(f *descriptorpb.FileDescriptorProto) (string, bool) {
opt := f.GetOptions()
if opt == nil {
return "", false
}
if !proto.HasExtension(opt, pbmap.E_DartPackage) {
return "", false
}
ext := proto.GetExtension(opt, pbmap.E_DartPackage)
sm, ok := ext.(*string)
if !ok {
return "", false
}
return *sm, true
}