goa_model_gen 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55c211e5cded1ceaad2db67152473f68f80db81ee97d83f8ea453ce08ee07cf8
|
4
|
+
data.tar.gz: 2a752f9dcd6daa746f7e8364ca9ca972e73a78e8dfd497eb08783da6ebb8ecc8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7197c6ef9959172c9d32f533fe8cacd72ed94f1b9f5a7eacbbed21622e39a5226cd9a2a765f25bed15e206cba6b8bb85a3e93e519596d6f667d7c22d9c5ada6a
|
7
|
+
data.tar.gz: bc4dfd74fd8cc1b0d7362218da8e51039614dcc008b074d95fab2fc5e6214f629be95a5917f3660621f655b65dbd864b50b0d58779345b18240d8e40a46fbde3
|
data/lib/goa_model_gen/field.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# coding: utf-8
|
1
2
|
require 'goa_model_gen'
|
2
3
|
require 'goa_model_gen/goa'
|
3
4
|
|
@@ -21,7 +22,7 @@ module GoaModelGen
|
|
21
22
|
end
|
22
23
|
|
23
24
|
# https://goa.design/design/overview/
|
24
|
-
PRIMITIVE_TYPES = %
|
25
|
+
PRIMITIVE_TYPES = %w[bool int int64 float string time.Time uuid.UUID *datastore.Key]
|
25
26
|
|
26
27
|
def goa_name
|
27
28
|
Goa.capitalize_join(name.split("_"))
|
@@ -78,8 +79,80 @@ module GoaModelGen
|
|
78
79
|
|
79
80
|
def golang_type
|
80
81
|
format2type = SWAGGER_TYPE_TO_GOLANG_TYPE[type]
|
81
|
-
raise "Golang type not found for #{
|
82
|
+
raise "Golang type not found for #{self.inspect}" unless format2type
|
82
83
|
return format2type[format]
|
83
84
|
end
|
85
|
+
|
86
|
+
def conv_func_part_for_model
|
87
|
+
conv_func_part_for(type, !!(/\A\*/ =~ type))
|
88
|
+
end
|
89
|
+
|
90
|
+
def conv_func_part_for_payload
|
91
|
+
conv_func_part_for(golang_type, nullable?)
|
92
|
+
end
|
93
|
+
|
94
|
+
def conv_func_part_for_media_type
|
95
|
+
conv_func_part_for(golang_type, nullable?)
|
96
|
+
end
|
97
|
+
|
98
|
+
def conv_func_part_for(value, with_pointer)
|
99
|
+
r = value.sub(/\A\*/, '').split('.').map(&:camelize).join
|
100
|
+
with_pointer ? "#{r}Pointer" : r
|
101
|
+
end
|
102
|
+
|
103
|
+
def payload_assignment_options(f)
|
104
|
+
if custom?
|
105
|
+
if type_obj && type_obj.base
|
106
|
+
if f.not_null?
|
107
|
+
return false, false, type # 型キャスト
|
108
|
+
else
|
109
|
+
st = f.golang_type.camelize
|
110
|
+
dt = type_obj.base.camelize
|
111
|
+
return false, false, ["#{st}PointerTo#{dt}", type] # ポインタを値にしてから型キャスト
|
112
|
+
end
|
113
|
+
else
|
114
|
+
return false, true, "#{type}PayloadToModel"
|
115
|
+
end
|
116
|
+
else
|
117
|
+
if type == f.golang_type
|
118
|
+
if f.not_null?
|
119
|
+
return true, nil, nil
|
120
|
+
else
|
121
|
+
return false, false, "#{f.conv_func_part_for_payload}To#{conv_func_part_for_model}"
|
122
|
+
end
|
123
|
+
else
|
124
|
+
with_error = (f.type == 'string')
|
125
|
+
return false, with_error, "#{f.conv_func_part_for_payload}To#{conv_func_part_for_model}"
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def media_type_assignment_options(f)
|
131
|
+
if custom?
|
132
|
+
if type_obj && type_obj.base
|
133
|
+
if f.not_null?
|
134
|
+
return false, false, type_obj.base # 型キャスト
|
135
|
+
else
|
136
|
+
st = type_obj.base.camelize
|
137
|
+
dt = f.golang_type.camelize
|
138
|
+
return false, false, [type_obj.base, "#{st}To#{dt}Pointer"] # 型キャストしてポインタを値に変換
|
139
|
+
end
|
140
|
+
else
|
141
|
+
return false, true, "#{type}ModelToMediaType"
|
142
|
+
end
|
143
|
+
else
|
144
|
+
if type == f.golang_type
|
145
|
+
if f.not_null?
|
146
|
+
return true, nil, nil
|
147
|
+
else
|
148
|
+
return false, false, "#{conv_func_part_for_model}To#{f.conv_func_part_for_payload}"
|
149
|
+
end
|
150
|
+
else
|
151
|
+
with_error = (type == 'string')
|
152
|
+
return false, with_error, "#{conv_func_part_for_model}To#{f.conv_func_part_for_payload}"
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
84
157
|
end
|
85
158
|
end
|
@@ -1,3 +1,12 @@
|
|
1
|
+
<%-
|
2
|
+
def method_calling_exp(m, argument)
|
3
|
+
if m.is_a?(Array)
|
4
|
+
m.reverse.join('(') + argument + (')' * m.length)
|
5
|
+
else
|
6
|
+
"#{m}(#{argument})"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
-%>
|
1
10
|
package controller
|
2
11
|
|
3
12
|
import (
|
@@ -7,71 +16,86 @@ import (
|
|
7
16
|
|
8
17
|
<%- types.select(&:gen_converter?).each do |type| -%>
|
9
18
|
<%- if type.payload -%>
|
10
|
-
func <%= type.name %>PayloadToModel(
|
11
|
-
|
12
|
-
|
19
|
+
func <%= type.name %>PayloadToModel(payload *app.<%= type.payload_name %>) (*model.<%= type.name %>, error) {
|
20
|
+
model := &model.<%= type.name %>{}
|
21
|
+
if err := CopyFrom<%= type.name %>PayloadToModel(payload, model); err != nil {
|
22
|
+
return nil, err
|
13
23
|
}
|
14
|
-
|
15
|
-
|
16
|
-
|
24
|
+
return model, nil
|
25
|
+
}
|
26
|
+
|
27
|
+
func CopyFrom<%= type.name %>PayloadToModel(payload *app.<%= type.payload_name %>, model *model.<%= type.name %>) error {
|
28
|
+
if payload == nil {
|
29
|
+
return NoPayloadGiven
|
30
|
+
}
|
31
|
+
if model == nil {
|
32
|
+
return NoModelGiven
|
33
|
+
}
|
17
34
|
|
35
|
+
<%-
|
18
36
|
type.fields_including_id.each do |f|
|
19
37
|
pf = type.payload.fields.detect{|pf| f.name.underscore == pf.name }
|
20
38
|
if pf.nil?
|
21
|
-
comments << "#{f.name} no payload field"
|
22
|
-
elsif f.custom?
|
23
39
|
-%>
|
24
|
-
|
25
|
-
<%-
|
26
|
-
|
27
|
-
|
28
|
-
|
40
|
+
// <%= f.name %> not found in payload fields
|
41
|
+
<%-
|
42
|
+
else
|
43
|
+
simple, with_error, method_name = f.payload_assignment_options(pf)
|
44
|
+
-%>
|
45
|
+
<%- if simple -%>
|
46
|
+
model.<%= f.name %> = payload.<%= f.name %>
|
47
|
+
<%- elsif !with_error -%>
|
48
|
+
model.<%= f.name %> = <%= method_calling_exp(method_name, "payload.#{f.name}") %>
|
49
|
+
<%- else -%>
|
50
|
+
if v, err := <%= method_calling_exp(method_name, "payload.#{f.name}") %>; err != nil {
|
51
|
+
return err
|
52
|
+
} else {
|
53
|
+
model.<%= f.name %> = v
|
54
|
+
}
|
55
|
+
<%- end -%>
|
29
56
|
<%- end -%>
|
30
57
|
<%- end -%>
|
31
|
-
<%-
|
32
|
-
|
33
|
-
<%- end -%>
|
34
|
-
<%- type.payload.field_diffs(type.fields.map{|f| f.name.underscore}).each do |pf| -%>
|
35
|
-
// No model field for payload field "<%= pf.name %>"
|
58
|
+
<%- type.payload.field_diffs(type.fields_including_id.map{|f| f.name.underscore}).each do |pf| -%>
|
59
|
+
// No model field for payload field "<%= pf.name %>"
|
36
60
|
<%- end -%>
|
37
|
-
|
61
|
+
return nil
|
38
62
|
}
|
39
63
|
|
40
64
|
<%- end -%>
|
41
65
|
<%- if type.media_type -%>
|
42
|
-
func <%= type.name %>ModelToMediaType(
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
<%-
|
48
|
-
comments = []
|
66
|
+
func <%= type.name %>ModelToMediaType(model *model.<%= type.name %>) (*app.<%= type.media_type_name_for_go %>, error) {
|
67
|
+
if model == nil {
|
68
|
+
return nil, NoModelGiven
|
69
|
+
}
|
70
|
+
r := &app.<%= type.media_type_name_for_go %>{}
|
49
71
|
|
72
|
+
<%-
|
50
73
|
type.fields_including_id.each do |f|
|
51
|
-
mf = type.media_type.fields.detect{|mf| f.name == mf.name
|
74
|
+
mf = type.media_type.fields.detect{|mf| f.name.underscore == mf.name }
|
52
75
|
if mf.nil?
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
76
|
+
-%>
|
77
|
+
// <%= f.name %> not found for media type field
|
78
|
+
<%-
|
79
|
+
else
|
80
|
+
simple, with_error, method_name = f.media_type_assignment_options(mf)
|
81
|
+
-%>
|
82
|
+
<%- if simple -%>
|
83
|
+
r.<%= mf.goa_name %> = model.<%= f.name %>
|
84
|
+
<%- elsif !with_error -%>
|
85
|
+
r.<%= mf.goa_name %> = <%= method_calling_exp(method_name, "model.#{f.name}") %>
|
57
86
|
<%- else -%>
|
58
|
-
|
87
|
+
if val, err := <%= method_calling_exp(method_name, "model.#{f.name}") %>; err != nil {
|
88
|
+
return nil, err
|
89
|
+
} else {
|
90
|
+
r.<%= mf.goa_name %> = val
|
91
|
+
}
|
59
92
|
<%- end
|
60
|
-
|
61
|
-
<%- if mf.golang_type == f.type -%>
|
62
|
-
<%= mf.goa_name %>: <%= mf.nullable? ? '&' : '' %>src.<%= f.name %>,
|
63
|
-
<%- else -%>
|
64
|
-
<%= mf.goa_name %>: <%= f.type.camelize %>To<%= mf.golang_type.camelize %><%= mf.nullable? ? 'Pointer' : '' %>(src.<%= f.name %>),
|
65
|
-
<%- end -%>
|
66
|
-
<%- end -%>
|
67
|
-
<%- end -%>
|
68
|
-
<%- comments.each do |comment| -%>
|
69
|
-
// <%= comment %>
|
93
|
+
end -%>
|
70
94
|
<%- end -%>
|
71
|
-
<%- type.media_type.field_diffs(type.
|
72
|
-
// No field for media type field "<%= mf.name %>"
|
95
|
+
<%- type.media_type.field_diffs(type.fields_including_id.map{|f| f.name.underscore}).each do |mf| -%>
|
96
|
+
// No model field for media type field "<%= mf.name %>"
|
73
97
|
<%- end -%>
|
74
|
-
|
98
|
+
return r, nil
|
75
99
|
}
|
76
100
|
|
77
101
|
<%- end -%>
|
@@ -1,9 +1,16 @@
|
|
1
1
|
package controller
|
2
2
|
|
3
3
|
import (
|
4
|
+
"fmt"
|
4
5
|
"strconv"
|
6
|
+
|
7
|
+
"google.golang.org/appengine/datastore"
|
5
8
|
)
|
6
9
|
|
10
|
+
var NoModelGiven = fmt.Errorf("No model given")
|
11
|
+
var NoPayloadGiven = fmt.Errorf("No payload given")
|
12
|
+
var NoMediaTypeGiven = fmt.Errorf("No media type given")
|
13
|
+
|
7
14
|
func BoolPointerToBool(v *bool) bool {
|
8
15
|
return BoolPointerToBoolWith(v, false)
|
9
16
|
}
|
@@ -58,3 +65,29 @@ func Int64ToStringPointer(v int64) *string {
|
|
58
65
|
s := Int64ToString(v)
|
59
66
|
return &s
|
60
67
|
}
|
68
|
+
|
69
|
+
func StringToDatastoreKeyPointer(v string) (*datastore.Key, error) {
|
70
|
+
return datastore.DecodeKey(v)
|
71
|
+
}
|
72
|
+
|
73
|
+
func StringPointerToDatastoreKeyPointer(v *string) (*datastore.Key, error) {
|
74
|
+
if v == nil {
|
75
|
+
return nil, nil
|
76
|
+
}
|
77
|
+
return StringToDatastoreKeyPointer(*v)
|
78
|
+
}
|
79
|
+
|
80
|
+
func DatastoreKeyPointerToString(key *datastore.Key) string {
|
81
|
+
if key == nil {
|
82
|
+
return ""
|
83
|
+
}
|
84
|
+
return key.Encode()
|
85
|
+
}
|
86
|
+
|
87
|
+
func DatastoreKeyPointerToStringPointer(key *datastore.Key) *string {
|
88
|
+
if key == nil {
|
89
|
+
return nil
|
90
|
+
}
|
91
|
+
s := DatastoreKeyPointerToString(key)
|
92
|
+
return &s
|
93
|
+
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: goa_model_gen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- akm
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|