goa_model_gen 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/goa_model_gen.gemspec +2 -0
- data/lib/goa_model_gen/cli.rb +18 -10
- data/lib/goa_model_gen/config.rb +4 -2
- data/lib/goa_model_gen/field.rb +7 -5
- data/lib/goa_model_gen/generator.rb +9 -4
- data/lib/goa_model_gen/source_file.rb +4 -0
- data/lib/goa_model_gen/templates/base.go.erb +7 -1
- data/lib/goa_model_gen/templates/converter.go.erb +14 -13
- data/lib/goa_model_gen/templates/converter_base.go.erb +25 -6
- data/lib/goa_model_gen/templates/goon.go.erb +3 -5
- data/lib/goa_model_gen/templates/model_validation.go.erb +0 -11
- data/lib/goa_model_gen/templates/{model_store.go.erb → store.go.erb} +44 -42
- data/lib/goa_model_gen/templates/store_validation.go.erb +20 -0
- data/lib/goa_model_gen/templates/validator.go.erb +3 -5
- data/lib/goa_model_gen/version.rb +1 -1
- metadata +33 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0cfb7caba6e01eee8a29002beafa8806c80c9b1acd47ac0d6d824a39b92f408b
|
4
|
+
data.tar.gz: e90a5a485bcc57521c408e3de2bc81a5d14e3fa62b2dea8695cf31efe858a2a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc42ddcd158483048bb69dd96b1e776dac7d11bbe4822bdfe5dc779d1c679c5d3f4feb65269a3053bce6cbf268defc560d547bc646a5c8cb217a5f67a8b97d4c
|
7
|
+
data.tar.gz: 5e606592d318569856b948d6d06f8927943e85ce6053337e18e880f47ddf1626fb8841ab7827247a4e510489fc96267be40681002cc2fc713fa6bcabb005d3d9
|
data/goa_model_gen.gemspec
CHANGED
@@ -30,4 +30,6 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.add_development_dependency "rake", "~> 10.0"
|
31
31
|
spec.add_development_dependency "rspec", "~> 3.0"
|
32
32
|
spec.add_development_dependency "pry"
|
33
|
+
spec.add_development_dependency "pry-byebug"
|
34
|
+
spec.add_development_dependency "pry-stack_explorer"
|
33
35
|
end
|
data/lib/goa_model_gen/cli.rb
CHANGED
@@ -37,14 +37,26 @@ module GoaModelGen
|
|
37
37
|
def model(*paths)
|
38
38
|
setup
|
39
39
|
new_generator.process({
|
40
|
-
"templates/goon.go.erb" => File.join(cfg.model_dir, "goon.go"),
|
41
40
|
'templates/validator.go.erb' => File.join(cfg.model_dir, 'validator.go'),
|
42
41
|
})
|
43
42
|
load_types_for(paths) do |source_file|
|
44
43
|
new_generator.tap{|g| g.source_file = source_file }.process({
|
45
|
-
'templates/model.go.erb' =>
|
46
|
-
'templates/
|
47
|
-
|
44
|
+
'templates/model.go.erb' => File.join(cfg.model_dir, "#{source_file.basename}.go"),
|
45
|
+
'templates/model_validation.go.erb' => File.join(cfg.model_dir, "#{source_file.basename}_validation.go"),
|
46
|
+
})
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
desc "store FILE1...", "Generate store files from definition files"
|
51
|
+
def store(*paths)
|
52
|
+
setup
|
53
|
+
new_generator.process({
|
54
|
+
"templates/goon.go.erb" => File.join(cfg.store_dir, "goon_store", "goon.go"),
|
55
|
+
})
|
56
|
+
load_types_for(paths) do |source_file|
|
57
|
+
new_generator.tap{|g| g.source_file = source_file }.process({
|
58
|
+
'templates/store.go.erb' => File.join(cfg.store_dir, source_file.basename, "store.go"),
|
59
|
+
'templates/store_validation.go.erb' => File.join(cfg.store_dir, source_file.basename, "validation.go"),
|
48
60
|
})
|
49
61
|
end
|
50
62
|
end
|
@@ -53,12 +65,12 @@ module GoaModelGen
|
|
53
65
|
def converter(*paths)
|
54
66
|
setup
|
55
67
|
new_generator.process({
|
56
|
-
"templates/converter_base.go.erb" => File.join(cfg.
|
68
|
+
"templates/converter_base.go.erb" => File.join(cfg.converter_dir, "converter_base.go"),
|
57
69
|
})
|
58
70
|
load_types_for(paths) do |source_file|
|
59
71
|
next if source_file.types.all?{|t| !t.payload && !t.media_type}
|
60
72
|
new_generator.tap{|g| g.source_file = source_file }.process({
|
61
|
-
'templates/converter.go.erb' =>
|
73
|
+
'templates/converter.go.erb' => File.join(cfg.converter_dir, source_file.basename, "conv.go"),
|
62
74
|
})
|
63
75
|
end
|
64
76
|
end
|
@@ -98,10 +110,6 @@ module GoaModelGen
|
|
98
110
|
yield(source_file)
|
99
111
|
end
|
100
112
|
end
|
101
|
-
|
102
|
-
def dest_path(dir, source_file, suffix)
|
103
|
-
File.join(dir, File.basename(source_file.yaml_path, ".*") + suffix)
|
104
|
-
end
|
105
113
|
end
|
106
114
|
|
107
115
|
end
|
data/lib/goa_model_gen/config.rb
CHANGED
@@ -14,7 +14,8 @@ module GoaModelGen
|
|
14
14
|
swagger_yaml
|
15
15
|
gofmt_disabled
|
16
16
|
model_dir
|
17
|
-
|
17
|
+
store_dir
|
18
|
+
converter_dir
|
18
19
|
validator_path
|
19
20
|
].freeze
|
20
21
|
|
@@ -25,7 +26,8 @@ module GoaModelGen
|
|
25
26
|
@swagger_yaml ||= "./swagger/swagger.yaml"
|
26
27
|
@gofmt_disabled ||= false
|
27
28
|
@model_dir ||= "./model"
|
28
|
-
@
|
29
|
+
@store_dir ||= "./stores"
|
30
|
+
@converter_dir ||= "./converters"
|
29
31
|
@validator_path ||= "gopkg.in/go-playground/validator.v9"
|
30
32
|
self
|
31
33
|
end
|
data/lib/goa_model_gen/field.rb
CHANGED
@@ -101,10 +101,7 @@ module GoaModelGen
|
|
101
101
|
# https://tour.golang.org/basics/11
|
102
102
|
# https://golang.org/pkg/go/types/#pkg-variables
|
103
103
|
SWAGGER_TYPE_TO_GOLANG_TYPE = {
|
104
|
-
"string" => Hash.new("string")
|
105
|
-
"date" => "time.Time",
|
106
|
-
"date-time" => "time.Time",
|
107
|
-
),
|
104
|
+
"string" => Hash.new("string"),
|
108
105
|
"number" => Hash.new("float32").update(
|
109
106
|
"double" => "float64",
|
110
107
|
),
|
@@ -118,8 +115,13 @@ module GoaModelGen
|
|
118
115
|
return format2type[format]
|
119
116
|
end
|
120
117
|
|
118
|
+
MODEL_FUNC_NAME_FILTERS = {
|
119
|
+
"TimeTime" => "Time",
|
120
|
+
}
|
121
|
+
|
121
122
|
def conv_func_part_for_model
|
122
|
-
conv_func_part_for(type, !!(/\A\*/ =~ type))
|
123
|
+
r = conv_func_part_for(type, !!(/\A\*/ =~ type))
|
124
|
+
MODEL_FUNC_NAME_FILTERS[r] || r
|
123
125
|
end
|
124
126
|
|
125
127
|
def conv_func_part_for_payload
|
@@ -35,17 +35,21 @@ module GoaModelGen
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def dependencies
|
38
|
-
@dependencies ||=
|
38
|
+
@dependencies ||= {}
|
39
39
|
end
|
40
40
|
|
41
41
|
def clear_dependencies
|
42
42
|
@dependencies = nil
|
43
43
|
end
|
44
44
|
|
45
|
-
def import(
|
46
|
-
|
47
|
-
|
45
|
+
def import(alias_or_package, package_or_nil = nil)
|
46
|
+
package = package_or_nil || alias_or_package
|
47
|
+
new_alias = package_or_nil ? alias_or_package.to_s : nil
|
48
|
+
if dependencies.key?(package)
|
49
|
+
old_alias = dependencies[package]
|
50
|
+
raise "Conflict alias #{old_alias.inspect} and #{new_alias.inspect}" if old_alias != new_alias
|
48
51
|
end
|
52
|
+
dependencies[package] ||= new_alias
|
49
53
|
end
|
50
54
|
|
51
55
|
def user_editable(value: true)
|
@@ -63,6 +67,7 @@ module GoaModelGen
|
|
63
67
|
|
64
68
|
def generate(template_path)
|
65
69
|
clear_dependencies
|
70
|
+
user_editable(value: false)
|
66
71
|
|
67
72
|
abs_path = File.expand_path('../' + template_path, __FILE__)
|
68
73
|
erb = ERB.new(File.read(abs_path), nil, "-")
|
@@ -9,7 +9,13 @@ package <%= package %>
|
|
9
9
|
|
10
10
|
<%- unless dependencies.empty? -%>
|
11
11
|
import (
|
12
|
-
<%=
|
12
|
+
<%=
|
13
|
+
golang_helper.partition(dependencies.keys).map do |group|
|
14
|
+
group.map do |path|
|
15
|
+
ailas_name = dependencies[path]
|
16
|
+
ailas_name ? "\t#{ailas_name} \"#{path}\"" : "\t\"#{path}\""
|
17
|
+
end.join("\n")
|
18
|
+
end.join("\n\n") %>
|
13
19
|
)
|
14
20
|
<%- end -%>
|
15
21
|
|
@@ -7,15 +7,16 @@ def method_calling_exp(m, argument)
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
-%>
|
10
|
-
<%- package
|
10
|
+
<%- package source_file.basename -%>
|
11
11
|
|
12
12
|
<%- source_file.types.select(&:gen_converter?).each do |type| -%>
|
13
13
|
<%- if type.payload -%>
|
14
14
|
<%-
|
15
|
-
import "#{config.go_package}/
|
15
|
+
import "#{config.go_package}/converters"
|
16
|
+
import :gen, "#{config.go_package}/gen/#{type.name.underscore}"
|
16
17
|
import "#{config.go_package}/model"
|
17
18
|
-%>
|
18
|
-
func <%= type.name %>PayloadToModel(payload *
|
19
|
+
func <%= type.name %>PayloadToModel(payload *gen.<%= type.payload_name %>) (*model.<%= type.name %>, error) {
|
19
20
|
model := &model.<%= type.name %>{}
|
20
21
|
if err := CopyFrom<%= type.name %>PayloadToModel(payload, model); err != nil {
|
21
22
|
return nil, err
|
@@ -23,12 +24,12 @@ func <%= type.name %>PayloadToModel(payload *app.<%= type.payload_name %>) (*mod
|
|
23
24
|
return model, nil
|
24
25
|
}
|
25
26
|
|
26
|
-
func CopyFrom<%= type.name %>PayloadToModel(payload *
|
27
|
+
func CopyFrom<%= type.name %>PayloadToModel(payload *gen.<%= type.payload_name %>, model *model.<%= type.name %>) error {
|
27
28
|
if payload == nil {
|
28
|
-
return NoPayloadGiven
|
29
|
+
return converters.NoPayloadGiven
|
29
30
|
}
|
30
31
|
if model == nil {
|
31
|
-
return NoModelGiven
|
32
|
+
return converters.NoModelGiven
|
32
33
|
}
|
33
34
|
|
34
35
|
<%-
|
@@ -44,9 +45,9 @@ func CopyFrom<%= type.name %>PayloadToModel(payload *app.<%= type.payload_name %
|
|
44
45
|
<%- if simple -%>
|
45
46
|
model.<%= f.name %> = payload.<%= f.goa_name %>
|
46
47
|
<%- elsif !with_error -%>
|
47
|
-
model.<%= f.name %> =
|
48
|
+
model.<%= f.name %> = converters.<%= method_calling_exp(method_name, "payload.#{f.goa_name}") %>
|
48
49
|
<%- else -%>
|
49
|
-
if v, err :=
|
50
|
+
if v, err := converters.<%= method_calling_exp(method_name, "payload.#{f.goa_name}") %>; err != nil {
|
50
51
|
return err
|
51
52
|
} else {
|
52
53
|
model.<%= f.name %> = v
|
@@ -62,11 +63,11 @@ func CopyFrom<%= type.name %>PayloadToModel(payload *app.<%= type.payload_name %
|
|
62
63
|
|
63
64
|
<%- end -%>
|
64
65
|
<%- if type.media_type -%>
|
65
|
-
func <%= type.name %>ModelToMediaType(model *model.<%= type.name %>) (*
|
66
|
+
func <%= type.name %>ModelToMediaType(model *model.<%= type.name %>) (*gen.<%= type.media_type_name_for_go %>, error) {
|
66
67
|
if model == nil {
|
67
|
-
return nil, NoModelGiven
|
68
|
+
return nil, converters.NoModelGiven
|
68
69
|
}
|
69
|
-
r := &
|
70
|
+
r := &gen.<%= type.media_type_name_for_go %>{}
|
70
71
|
|
71
72
|
<%-
|
72
73
|
type.fields_including_id.each do |f|
|
@@ -81,9 +82,9 @@ func <%= type.name %>ModelToMediaType(model *model.<%= type.name %>) (*app.<%= t
|
|
81
82
|
<%- if simple -%>
|
82
83
|
r.<%= f.goa_name %> = model.<%= f.name %>
|
83
84
|
<%- elsif !with_error -%>
|
84
|
-
r.<%= f.goa_name %> =
|
85
|
+
r.<%= f.goa_name %> = converters.<%= method_calling_exp(method_name, "model.#{f.name}") %>
|
85
86
|
<%- else -%>
|
86
|
-
if val, err :=
|
87
|
+
if val, err := converters.<%= method_calling_exp(method_name, "model.#{f.name}") %>; err != nil {
|
87
88
|
return nil, err
|
88
89
|
} else {
|
89
90
|
r.<%= f.goa_name %> = val
|
@@ -1,11 +1,10 @@
|
|
1
|
-
<%- package '
|
1
|
+
<%- package 'converters' -%>
|
2
2
|
|
3
3
|
<%-
|
4
|
-
import
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
)
|
4
|
+
import "fmt"
|
5
|
+
import "strconv"
|
6
|
+
import "time"
|
7
|
+
import "google.golang.org/appengine/datastore"
|
9
8
|
-%>
|
10
9
|
|
11
10
|
var NoModelGiven = fmt.Errorf("No model given")
|
@@ -96,3 +95,23 @@ func DatastoreKeyPointerToStringPointer(key *datastore.Key) *string {
|
|
96
95
|
s := DatastoreKeyPointerToString(key)
|
97
96
|
return &s
|
98
97
|
}
|
98
|
+
|
99
|
+
func StringToTime(v string) (time.Time, error) {
|
100
|
+
return time.Parse(time.RFC3339, v)
|
101
|
+
}
|
102
|
+
|
103
|
+
func StringPointerToTime(v *string) (time.Time, error) {
|
104
|
+
if v == nil {
|
105
|
+
return time.Time{}, nil
|
106
|
+
}
|
107
|
+
return StringToTime(*v)
|
108
|
+
}
|
109
|
+
|
110
|
+
func TimeToString(t time.Time) string {
|
111
|
+
return t.Format(time.RFC3339)
|
112
|
+
}
|
113
|
+
|
114
|
+
func TimeToStringPointer(t time.Time) *string {
|
115
|
+
s := TimeToString(t)
|
116
|
+
return &s
|
117
|
+
}
|
@@ -1,11 +1,9 @@
|
|
1
1
|
<%- user_editable -%>
|
2
|
-
<%- package '
|
2
|
+
<%- package 'goon_store' -%>
|
3
3
|
|
4
4
|
<%-
|
5
|
-
import
|
6
|
-
|
7
|
-
"github.com/mjibson/goon",
|
8
|
-
)
|
5
|
+
import "golang.org/x/net/context"
|
6
|
+
import "github.com/mjibson/goon"
|
9
7
|
-%>
|
10
8
|
|
11
9
|
// // Use the following code if you want to change kind name from model struct name.
|
@@ -6,17 +6,6 @@
|
|
6
6
|
import "context"
|
7
7
|
import config.validator_path # "gopkg.in/go-playground/validator.v9"
|
8
8
|
-%>
|
9
|
-
<%- store_name = "#{model.name}Store" -%>
|
10
|
-
func (s *<%= store_name %>) Validate(ctx context.Context, m *<%= model.name %>) error {
|
11
|
-
if err := m.Validate(ctx); err != nil {
|
12
|
-
return err
|
13
|
-
}
|
14
|
-
if err := s.ValidateUniqueness(ctx, m); err != nil {
|
15
|
-
return err
|
16
|
-
}
|
17
|
-
return nil
|
18
|
-
}
|
19
|
-
|
20
9
|
func (m *<%= model.name %>) Validate(ctx context.Context) error {
|
21
10
|
return WithValidator(ctx, func(validate *validator.Validate) error {
|
22
11
|
return validate.Struct(m)
|
@@ -1,14 +1,16 @@
|
|
1
|
-
<%- package
|
1
|
+
<%- package source_file.basename -%>
|
2
2
|
|
3
3
|
<%- source_file.types.select(&:store?).each do |model| -%>
|
4
4
|
<%- store_name = "#{model.name}Store" -%>
|
5
5
|
<%-
|
6
|
-
import
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
6
|
+
import "context"
|
7
|
+
import "fmt"
|
8
|
+
import "google.golang.org/appengine/datastore"
|
9
|
+
import "google.golang.org/appengine/log"
|
10
|
+
import "#{config.go_package}/model"
|
11
|
+
import :goon, "#{config.go_package}/stores/goon_store"
|
12
|
+
|
13
|
+
model_name = "model.#{model.name}"
|
12
14
|
-%>
|
13
15
|
type <%= store_name %> struct{
|
14
16
|
<%- if model.parent -%>
|
@@ -16,44 +18,44 @@ type <%= store_name %> struct{
|
|
16
18
|
<%- end -%>
|
17
19
|
}
|
18
20
|
|
19
|
-
func (s *<%= store_name %>) All(ctx context.Context) ([]*<%=
|
21
|
+
func (s *<%= store_name %>) All(ctx context.Context) ([]*<%= model_name %>, error) {
|
20
22
|
return s.Select(ctx, s.Query(ctx))
|
21
23
|
}
|
22
24
|
|
23
|
-
func (s *<%= store_name %>) Select(ctx context.Context, q *datastore.Query) ([]*<%=
|
24
|
-
g :=
|
25
|
-
r := []*<%=
|
25
|
+
func (s *<%= store_name %>) Select(ctx context.Context, q *datastore.Query) ([]*<%= model_name %>, error) {
|
26
|
+
g := goon.FromContext(ctx)
|
27
|
+
r := []*<%= model_name %>{}
|
26
28
|
log.Infof(ctx, "q is %v\n", q)
|
27
29
|
_, err := g.GetAll(q.EventualConsistency(), &r)
|
28
30
|
if err != nil {
|
29
|
-
log.Errorf(ctx, "Failed to Select <%=
|
31
|
+
log.Errorf(ctx, "Failed to Select <%= model_name %> because of %v\n", err)
|
30
32
|
return nil, err
|
31
33
|
}
|
32
34
|
return r, nil
|
33
35
|
}
|
34
36
|
|
35
37
|
func (s *<%= store_name %>) CountBy(ctx context.Context, q *datastore.Query) (int, error) {
|
36
|
-
g :=
|
38
|
+
g := goon.FromContext(ctx)
|
37
39
|
c, err := g.Count(q)
|
38
40
|
if err != nil {
|
39
|
-
log.Errorf(ctx, "Failed to count <%=
|
41
|
+
log.Errorf(ctx, "Failed to count <%= model_name %> with %v because of %v\n", q, err)
|
40
42
|
return 0, err
|
41
43
|
}
|
42
44
|
return c, nil
|
43
45
|
}
|
44
46
|
|
45
47
|
func (s *<%= store_name %>) Query(ctx context.Context) *datastore.Query {
|
46
|
-
g :=
|
47
|
-
k := g.Kind(new(<%=
|
48
|
-
// log.Infof(ctx, "Kind for <%=
|
48
|
+
g := goon.FromContext(ctx)
|
49
|
+
k := g.Kind(new(<%= model_name %>))
|
50
|
+
// log.Infof(ctx, "Kind for <%= model_name %> is %v\n", k)
|
49
51
|
return datastore.NewQuery(k)
|
50
52
|
}
|
51
53
|
|
52
|
-
func (s *<%= store_name %>) ByID(ctx context.Context, <%= model.id_name_var %> <%= model.id_golang_type %>) (*<%=
|
54
|
+
func (s *<%= store_name %>) ByID(ctx context.Context, <%= model.id_name_var %> <%= model.id_golang_type %>) (*<%= model_name %>, error) {
|
53
55
|
<%- if model.parent -%>
|
54
|
-
r := <%=
|
56
|
+
r := <%= model_name %>{ParentKey: s.ParentKey, <%= model.id_name %>: <%= model.id_name_var %>}
|
55
57
|
<%- else -%>
|
56
|
-
r := <%=
|
58
|
+
r := <%= model_name %>{<%= model.id_name %>: <%= model.id_name_var %>}
|
57
59
|
<%- end -%>
|
58
60
|
err := s.Get(ctx, &r)
|
59
61
|
if err != nil {
|
@@ -62,16 +64,16 @@ func (s *<%= store_name %>) ByID(ctx context.Context, <%= model.id_name_var %> <
|
|
62
64
|
return &r, nil
|
63
65
|
}
|
64
66
|
|
65
|
-
func (s *<%= store_name %>) ByKey(ctx context.Context, key *datastore.Key) (*<%=
|
67
|
+
func (s *<%= store_name %>) ByKey(ctx context.Context, key *datastore.Key) (*<%= model_name %>, error) {
|
66
68
|
if err := s.IsValidKey(ctx, key); err != nil {
|
67
69
|
log.Errorf(ctx, "<%= store_name %>.ByKey got Invalid key: %v because of %v\n", key, err)
|
68
70
|
return nil, err
|
69
71
|
}
|
70
72
|
|
71
73
|
<%- if model.parent -%>
|
72
|
-
r := <%=
|
74
|
+
r := <%= model_name %>{ParentKey: key.Parent(), <%= model.id_name %>: key.<%= model.key_id_method %>()}
|
73
75
|
<%- else -%>
|
74
|
-
r := <%=
|
76
|
+
r := <%= model_name %>{<%= model.id_name %>: key.<%= model.key_id_method %>()}
|
75
77
|
<%- end -%>
|
76
78
|
err := s.Get(ctx, &r)
|
77
79
|
if err != nil {
|
@@ -80,16 +82,16 @@ func (s *<%= store_name %>) ByKey(ctx context.Context, key *datastore.Key) (*<%=
|
|
80
82
|
return &r, nil
|
81
83
|
}
|
82
84
|
|
83
|
-
func (s *<%= store_name %>) Get(ctx context.Context, m *<%=
|
84
|
-
g :=
|
85
|
+
func (s *<%= store_name %>) Get(ctx context.Context, m *<%= model_name %>) error {
|
86
|
+
g := goon.FromContext(ctx)
|
85
87
|
err := g.Get(m)
|
86
88
|
if err != nil {
|
87
|
-
log.Errorf(ctx, "Failed to Get <%=
|
89
|
+
log.Errorf(ctx, "Failed to Get <%= model_name %> because of %v\n", err)
|
88
90
|
return err
|
89
91
|
}
|
90
92
|
<%- if model.parent -%>
|
91
93
|
if err := s.ValidateParent(m); err != nil {
|
92
|
-
log.Errorf(ctx, "Invalid parent key for <%=
|
94
|
+
log.Errorf(ctx, "Invalid parent key for <%= model_name %> because of %v\n", err)
|
93
95
|
return err
|
94
96
|
}
|
95
97
|
<%- end -%>
|
@@ -101,8 +103,8 @@ func (s *<%= store_name %>) IsValidKey(ctx context.Context, key *datastore.Key)
|
|
101
103
|
if key == nil {
|
102
104
|
return fmt.Errorf("key is nil")
|
103
105
|
}
|
104
|
-
g :=
|
105
|
-
expected := g.Kind(&<%=
|
106
|
+
g := goon.FromContext(ctx)
|
107
|
+
expected := g.Kind(&<%= model_name %>{})
|
106
108
|
if key.Kind() != expected {
|
107
109
|
return fmt.Errorf("key kind must be %s but was %s", expected, key.Kind())
|
108
110
|
}
|
@@ -114,11 +116,11 @@ func (s *<%= store_name %>) IsValidKey(ctx context.Context, key *datastore.Key)
|
|
114
116
|
return nil
|
115
117
|
}
|
116
118
|
|
117
|
-
func (s *<%= store_name %>) Exist(ctx context.Context, m *<%=
|
119
|
+
func (s *<%= store_name %>) Exist(ctx context.Context, m *<%= model_name %>) (bool, error) {
|
118
120
|
if m.ID == <%= model.id_field.zero_value_expression %> {
|
119
121
|
return false, nil
|
120
122
|
}
|
121
|
-
g :=
|
123
|
+
g := goon.FromContext(ctx)
|
122
124
|
key, err := g.KeyError(m)
|
123
125
|
if err != nil {
|
124
126
|
log.Errorf(ctx, "Failed to Get Key of %v because of %v\n", m, err)
|
@@ -135,7 +137,7 @@ func (s *<%= store_name %>) Exist(ctx context.Context, m *<%= model.name %>) (bo
|
|
135
137
|
}
|
136
138
|
}
|
137
139
|
|
138
|
-
func (s *<%= store_name %>) Create(ctx context.Context, m *<%=
|
140
|
+
func (s *<%= store_name %>) Create(ctx context.Context, m *<%= model_name %>) (*datastore.Key, error) {
|
139
141
|
if err := m.PrepareToCreate(); err != nil {
|
140
142
|
return nil, err
|
141
143
|
}
|
@@ -152,7 +154,7 @@ func (s *<%= store_name %>) Create(ctx context.Context, m *<%= model.name %>) (*
|
|
152
154
|
})
|
153
155
|
}
|
154
156
|
|
155
|
-
func (s *<%= store_name %>) Update(ctx context.Context, m *<%=
|
157
|
+
func (s *<%= store_name %>) Update(ctx context.Context, m *<%= model_name %>) (*datastore.Key, error) {
|
156
158
|
if err := m.PrepareToUpdate(); err != nil {
|
157
159
|
return nil, err
|
158
160
|
}
|
@@ -169,7 +171,7 @@ func (s *<%= store_name %>) Update(ctx context.Context, m *<%= model.name %>) (*
|
|
169
171
|
})
|
170
172
|
}
|
171
173
|
|
172
|
-
func (s *<%= store_name %>) PutWith(ctx context.Context, m *<%=
|
174
|
+
func (s *<%= store_name %>) PutWith(ctx context.Context, m *<%= model_name %>, f func() error) (*datastore.Key, error) {
|
173
175
|
if err := s.Validate(ctx, m); err != nil {
|
174
176
|
return nil, err
|
175
177
|
}
|
@@ -182,7 +184,7 @@ func (s *<%= store_name %>) PutWith(ctx context.Context, m *<%= model.name %>, f
|
|
182
184
|
return s.Put(ctx, m)
|
183
185
|
}
|
184
186
|
|
185
|
-
func (s *<%= store_name %>) Put(ctx context.Context, m *<%=
|
187
|
+
func (s *<%= store_name %>) Put(ctx context.Context, m *<%= model_name %>) (*datastore.Key, error) {
|
186
188
|
<%- if model.goon['id_type'] == 'UUID' -%>
|
187
189
|
<%- import "github.com/goadesign/goa/uuid" -%>
|
188
190
|
if m.Id == "" {
|
@@ -191,11 +193,11 @@ func (s *<%= store_name %>) Put(ctx context.Context, m *<%= model.name %>) (*dat
|
|
191
193
|
<%- end -%>
|
192
194
|
<%- if model.parent -%>
|
193
195
|
if err := s.ValidateParent(m); err != nil {
|
194
|
-
log.Errorf(ctx, "Invalid parent key for <%=
|
196
|
+
log.Errorf(ctx, "Invalid parent key for <%= model_name %> because of %v\n", err)
|
195
197
|
return nil, err
|
196
198
|
}
|
197
199
|
<%- end -%>
|
198
|
-
g :=
|
200
|
+
g := goon.FromContext(ctx)
|
199
201
|
key, err := g.Put(m)
|
200
202
|
if err != nil {
|
201
203
|
log.Errorf(ctx, "Failed to Put %v because of %v\n", m, err)
|
@@ -205,7 +207,7 @@ func (s *<%= store_name %>) Put(ctx context.Context, m *<%= model.name %>) (*dat
|
|
205
207
|
}
|
206
208
|
|
207
209
|
<%- if model.parent -%>
|
208
|
-
func (s *<%= store_name %>) ValidateParent(m *<%=
|
210
|
+
func (s *<%= store_name %>) ValidateParent(m *<%= model_name %>) error {
|
209
211
|
if s.ParentKey == nil {
|
210
212
|
return nil
|
211
213
|
}
|
@@ -219,8 +221,8 @@ func (s *<%= store_name %>) ValidateParent(m *<%= model.name %>) error {
|
|
219
221
|
}
|
220
222
|
|
221
223
|
<%- end -%>
|
222
|
-
func (s *<%= store_name %>) Delete(ctx context.Context, m *<%=
|
223
|
-
g :=
|
224
|
+
func (s *<%= store_name %>) Delete(ctx context.Context, m *<%= model_name %>) error {
|
225
|
+
g := goon.FromContext(ctx)
|
224
226
|
key, err := g.KeyError(m)
|
225
227
|
if err != nil {
|
226
228
|
log.Errorf(ctx, "Failed to Get key of %v because of %v\n", m, err)
|
@@ -233,7 +235,7 @@ func (s *<%= store_name %>) Delete(ctx context.Context, m *<%= model.name %>) er
|
|
233
235
|
return nil
|
234
236
|
}
|
235
237
|
|
236
|
-
func (s *<%= store_name %>) ValidateUniqueness(ctx context.Context, m *<%=
|
238
|
+
func (s *<%= store_name %>) ValidateUniqueness(ctx context.Context, m *<%= model_name %>) error {
|
237
239
|
conditions := map[string]interface{}{
|
238
240
|
<%- model.fields.select(&:unique?).each do |field| -%>
|
239
241
|
"<%= field.name %>": m.<%= field.name %>,
|
@@ -250,7 +252,7 @@ func (s *<%= store_name %>) ValidateUniqueness(ctx context.Context, m *<%= model
|
|
250
252
|
b = 1
|
251
253
|
}
|
252
254
|
if c > b {
|
253
|
-
return &ValidationError{
|
255
|
+
return &model.ValidationError{
|
254
256
|
Field: field,
|
255
257
|
Message: fmt.Sprintf("%v has already been taken", value),
|
256
258
|
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
<%- user_editable -%>
|
2
|
+
<%- package source_file.basename -%>
|
3
|
+
|
4
|
+
<%- source_file.types.select(&:store?).each do |model| -%>
|
5
|
+
<%-
|
6
|
+
import "context"
|
7
|
+
import "#{config.go_package}/model"
|
8
|
+
-%>
|
9
|
+
<%- store_name = "#{model.name}Store" -%>
|
10
|
+
func (s *<%= store_name %>) Validate(ctx context.Context, m *model.<%= model.name %>) error {
|
11
|
+
if err := m.Validate(ctx); err != nil {
|
12
|
+
return err
|
13
|
+
}
|
14
|
+
if err := s.ValidateUniqueness(ctx, m); err != nil {
|
15
|
+
return err
|
16
|
+
}
|
17
|
+
return nil
|
18
|
+
}
|
19
|
+
|
20
|
+
<%- end -%>
|
@@ -1,11 +1,9 @@
|
|
1
1
|
<%- user_editable -%>
|
2
2
|
<%- package "model" -%>
|
3
3
|
<%-
|
4
|
-
import
|
5
|
-
|
6
|
-
|
7
|
-
# ja_translations "github.com/akm/validator/translations/ja",
|
8
|
-
)
|
4
|
+
import "context"
|
5
|
+
import config.validator_path
|
6
|
+
# import ja_translations "github.com/akm/validator/translations/ja"
|
9
7
|
-%>
|
10
8
|
|
11
9
|
func NewValidator(ctx context.Context) (*validator.Validate, error) {
|
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.7.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-12-
|
11
|
+
date: 2018-12-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -94,6 +94,34 @@ dependencies:
|
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry-byebug
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry-stack_explorer
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
97
125
|
description: Generate model files for goa in golang
|
98
126
|
email:
|
99
127
|
- akm2000@gmail.com
|
@@ -129,8 +157,9 @@ files:
|
|
129
157
|
- lib/goa_model_gen/templates/converter_base.go.erb
|
130
158
|
- lib/goa_model_gen/templates/goon.go.erb
|
131
159
|
- lib/goa_model_gen/templates/model.go.erb
|
132
|
-
- lib/goa_model_gen/templates/model_store.go.erb
|
133
160
|
- lib/goa_model_gen/templates/model_validation.go.erb
|
161
|
+
- lib/goa_model_gen/templates/store.go.erb
|
162
|
+
- lib/goa_model_gen/templates/store_validation.go.erb
|
134
163
|
- lib/goa_model_gen/templates/validator.go.erb
|
135
164
|
- lib/goa_model_gen/type.rb
|
136
165
|
- lib/goa_model_gen/version.rb
|
@@ -154,7 +183,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
183
|
version: '0'
|
155
184
|
requirements: []
|
156
185
|
rubyforge_project:
|
157
|
-
rubygems_version: 2.7.
|
186
|
+
rubygems_version: 2.7.6
|
158
187
|
signing_key:
|
159
188
|
specification_version: 4
|
160
189
|
summary: Generate model files for goa in golang
|