-
Notifications
You must be signed in to change notification settings - Fork 171
/
proto-to-form.go
167 lines (154 loc) · 4.55 KB
/
proto-to-form.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package protos
import (
"reflect"
"strings"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"github.com/pydio/cells/v4/common/forms"
)
func GenerateProtoToForm(prefix string, msg interface{}, makeSwitch bool, i18nKeys ...map[string]string) *forms.Form {
s := reflect.ValueOf(msg).Elem()
isProto := false
var fieldsDescriptors protoreflect.FieldDescriptors
if pMess, ok := msg.(proto.Message); ok {
isProto = true
fieldsDescriptors = pMess.ProtoReflect().Descriptor().Fields()
}
g := &forms.Group{}
sw := &forms.SwitchField{
Name: "fieldname",
Label: "Field Name",
Values: []*forms.SwitchValue{},
}
for i := 0; i < s.NumField(); i++ {
value := s.Field(i)
valueField := s.Type().Field(i)
var fieldName = valueField.Name
if isProto && (fieldName == "sizeCache" || fieldName == "state" || fieldName == "unknownFields") {
continue
}
if jsonTag := valueField.Tag.Get("json"); jsonTag != "" && jsonTag != "-" {
// check for possible comma as in "...,omitempty"
var commaIdx int
if commaIdx = strings.Index(jsonTag, ","); commaIdx < 0 {
commaIdx = len(jsonTag)
}
fieldName = jsonTag[:commaIdx]
}
var fd protoreflect.FieldDescriptor
if isProto {
fd = fieldsDescriptors.ByTextName(fieldName)
}
if field := fieldForValue(prefix, fieldName, value.Kind(), value.Type(), fd, i18nKeys...); field != nil {
if f, ok := field.(*forms.ReplicableFields); ok && makeSwitch {
f.Mandatory = true
field = f
}
g.Fields = append(g.Fields, field)
sw.Values = append(sw.Values, &forms.SwitchValue{
Name: fieldName,
Value: fieldName,
Label: prefix + "." + valueField.Name,
Fields: []forms.Field{field},
})
if len(i18nKeys) > 0 {
i18nKeys[0][prefix+"."+valueField.Name] = valueField.Name
}
}
}
if makeSwitch {
// return a unique switch
g.Fields = []forms.Field{sw}
}
f := &forms.Form{
Groups: []*forms.Group{g},
}
return f
}
func fieldForValue(prefix, name string, kind reflect.Kind, vType reflect.Type, fd protoreflect.FieldDescriptor, i18nKeys ...map[string]string) forms.Field {
if len(i18nKeys) > 0 {
i18nKeys[0][prefix+"."+name] = name
}
switch kind {
case reflect.String:
return &forms.FormField{
Name: name,
Type: "string",
Label: prefix + "." + name,
}
case reflect.Bool:
return &forms.FormField{
Name: name,
Type: "boolean",
Label: prefix + "." + name,
}
case reflect.Int64:
return &forms.FormField{
Name: name,
Type: "integer",
Label: prefix + "." + name,
}
case reflect.Int32:
if fd != nil {
if eDesc := fd.Enum(); eDesc != nil {
var mss []map[string]string
eValues := eDesc.Values()
for k := 0; k < eValues.Len(); k++ {
eVal := string(eValues.Get(k).Name())
kV := make(map[string]string, 1)
kV[eVal] = prefix + "." + name + "." + eVal
mss = append(mss, kV)
if len(i18nKeys) > 0 {
i18nKeys[0][prefix+"."+name+"."+eVal] = eVal
}
}
selector := &forms.FormField{
Name: name,
Type: "select",
Label: prefix + "." + name,
ChoicePresetList: mss,
}
return selector
}
}
return &forms.FormField{
Name: name,
Type: "integer",
Label: prefix + "." + name,
}
case reflect.Slice:
//fmt.Println("Slice of", value.Type().Elem().Kind())
sKind := vType.Elem().Kind()
replicable := &forms.ReplicableFields{Id: name, Title: name + "[]"}
if field := fieldForValue(prefix, name, sKind, vType, fd, i18nKeys...); field != nil {
replicable.Fields = append(replicable.Fields, field)
return replicable
}
return nil
case reflect.Ptr:
//fmt.Println("Struct of", kind, vType)
return nil
default:
//fmt.Println("Unkown type: ", name, kind)
return nil
}
}