goa_model_gen 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/goa_model_gen/cli.rb +7 -2
- data/lib/goa_model_gen/field.rb +10 -0
- data/lib/goa_model_gen/generator.rb +25 -23
- data/lib/goa_model_gen/templates/converter_base.go.erb +4 -0
- data/lib/goa_model_gen/templates/model.go.erb +4 -0
- data/lib/goa_model_gen/templates/model_store.go.erb +12 -7
- data/lib/goa_model_gen/type.rb +4 -1
- data/lib/goa_model_gen/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb100e41bfae1d78b4c222638a59945e42efa13af022c000d55efa3be6d71478
|
4
|
+
data.tar.gz: 53d689f9969dda1338ef1d177e56f1da34d04c40e55433e8e1816f8b8fea74de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c93f6c04d0fd3f4a1b72856e19c9a8ad026c2253cd14e9632729c075d0e70277f62464d03532cf90a2bd88326d2a4f9df528c3cfce923a46d8a4bcb5d2d12882
|
7
|
+
data.tar.gz: a163ecc4e61e8b03aa8de557a12c029b71a7e8253d910fe45df8c248c9caf4db457c1805a008c5d2bfa6bc870608f9833d047142aacccde45bdd6d9027fdc959
|
data/lib/goa_model_gen/cli.rb
CHANGED
@@ -9,9 +9,12 @@ require "goa_model_gen/generator"
|
|
9
9
|
|
10
10
|
module GoaModelGen
|
11
11
|
class Cli < Thor
|
12
|
+
include Thor::Actions
|
13
|
+
|
12
14
|
class_option :version, type: :boolean, aliases: 'v', desc: 'Show version before processing'
|
13
|
-
class_option :
|
15
|
+
class_option :skip, type: :boolean, aliases: 's', desc: "Skip generate file"
|
14
16
|
class_option :force, type: :boolean, aliases: 'f', desc: 'Force overwrite files'
|
17
|
+
class_option :keep_editable, type: :boolean, aliases: 'k', default: true, desc: 'Keep user editable file'
|
15
18
|
class_option :log_level, type: :string, aliases: 'l', desc: 'Log level, one of debug,info,warn,error,fatal. The default value is info'
|
16
19
|
class_option :config, type: :string, aliases: 'c', default: './goa_model_gen.yaml', desc: 'Path to config file. You can generate it by config subcommand'
|
17
20
|
|
@@ -81,8 +84,10 @@ module GoaModelGen
|
|
81
84
|
|
82
85
|
def new_generator
|
83
86
|
GoaModelGen::Generator.new(cfg).tap do |g|
|
87
|
+
g.thor = self
|
84
88
|
g.force = options[:force]
|
85
|
-
g.
|
89
|
+
g.skip = options[:skip]
|
90
|
+
g.keep_editable = options[:keep_editable]
|
86
91
|
end
|
87
92
|
end
|
88
93
|
|
data/lib/goa_model_gen/field.rb
CHANGED
@@ -62,6 +62,16 @@ module GoaModelGen
|
|
62
62
|
!not_null?
|
63
63
|
end
|
64
64
|
|
65
|
+
def zero_value_expression
|
66
|
+
case type
|
67
|
+
when 'bool' then 'false'
|
68
|
+
when 'int', 'int32', 'int64',
|
69
|
+
'float', 'float32', 'float64' then '0'
|
70
|
+
when 'string', 'UUID' then '""'
|
71
|
+
else raise "Unsupproted zero value for #{type}"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
65
75
|
def assign_type_base(types)
|
66
76
|
@type_obj = types[self.type]
|
67
77
|
end
|
@@ -10,14 +10,16 @@ module GoaModelGen
|
|
10
10
|
class Generator
|
11
11
|
# These are used in templates
|
12
12
|
attr_reader :config
|
13
|
+
attr_accessor :thor
|
13
14
|
attr_accessor :source_file
|
14
|
-
attr_accessor :force, :
|
15
|
+
attr_accessor :force, :skip
|
16
|
+
attr_accessor :keep_editable
|
15
17
|
|
16
18
|
def initialize(config)
|
17
19
|
@config = config
|
18
20
|
@user_editable = false
|
19
21
|
@force = false
|
20
|
-
@
|
22
|
+
@skip = false
|
21
23
|
end
|
22
24
|
|
23
25
|
def golang_helper
|
@@ -71,35 +73,35 @@ module GoaModelGen
|
|
71
73
|
|
72
74
|
base = ERB.new(File.read(GO_BASE_PATH), nil, "-")
|
73
75
|
base.filename = GO_BASE_PATH
|
74
|
-
base.result(binding).strip
|
76
|
+
r = base.result(binding).strip << "\n"
|
77
|
+
r = gofmt(r) unless config.gofmt_disabled
|
78
|
+
return r
|
79
|
+
end
|
80
|
+
|
81
|
+
def gofmt(content)
|
82
|
+
# https://docs.ruby-lang.org/ja/2.5.0/class/IO.html#S_POPEN
|
83
|
+
return IO.popen("gofmt", "r+") do |io|
|
84
|
+
io.puts(content)
|
85
|
+
io.close_write
|
86
|
+
io.read
|
87
|
+
end
|
75
88
|
end
|
76
89
|
|
77
90
|
COLORS = {
|
78
|
-
|
79
|
-
|
80
|
-
overwrite: "\e[33m", # yellow # file_exist && !user_editable
|
81
|
-
keep: "\e[34m", # blue # file_exist && user_editable && !force
|
82
|
-
force_overwrite: "\e[31m", # red # file_exist && user_editable && force
|
83
|
-
clear: "\e[0m", # clear
|
91
|
+
blue: "\e[34m",
|
92
|
+
clear: "\e[0m",
|
84
93
|
}
|
85
|
-
MAX_ACTION_LENGTH = COLORS.keys.map(&:to_s).map(&:length).max
|
86
94
|
|
87
95
|
def run(template_path, output_path)
|
88
|
-
already_exist = File.exist?(output_path)
|
89
96
|
content = generate(template_path)
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
!user_editable? ? :overwrite :
|
95
|
-
force ? :force_overwrite : :keep
|
96
|
-
GoaModelGen.logger.info("%s%-#{MAX_ACTION_LENGTH}s %s%s" % [COLORS[action], action.to_s, output_path, COLORS[:clear]])
|
97
|
-
return if action == :no_change
|
98
|
-
return if dryrun
|
99
|
-
open(output_path, 'w'){|f| f.puts(content) }
|
100
|
-
if (File.extname(output_path) == '.go') && !config.gofmt_disabled
|
101
|
-
system("gofmt -w #{output_path}")
|
97
|
+
|
98
|
+
if user_editable? && keep_editable
|
99
|
+
$stderr.puts("%sKEEP%s %s" % [COLORS[:blue], COLORS[:clear], output_path])
|
100
|
+
return
|
102
101
|
end
|
102
|
+
|
103
|
+
options = {skip: skip, force: force}
|
104
|
+
thor.create_file(output_path, content, options)
|
103
105
|
end
|
104
106
|
|
105
107
|
def process(temp_path_to_dest_path)
|
@@ -55,6 +55,10 @@ func (m *<%= model.name %>) PrepareToUpdate() error {
|
|
55
55
|
return nil
|
56
56
|
}
|
57
57
|
|
58
|
+
func (m *<%= model.name %>) IsPersisted() bool {
|
59
|
+
return m.ID != <%= model.id_field.zero_value_expression %>
|
60
|
+
}
|
61
|
+
|
58
62
|
<%- if model.parent -%>
|
59
63
|
<%- import "context" -%>
|
60
64
|
func (m *<%= model.name %>) Parent(ctx context.Context) (*<%= model.parent %>, error) {
|
@@ -115,6 +115,9 @@ func (s *<%= store_name %>) IsValidKey(ctx context.Context, key *datastore.Key)
|
|
115
115
|
}
|
116
116
|
|
117
117
|
func (s *<%= store_name %>) Exist(ctx context.Context, m *<%= model.name %>) (bool, error) {
|
118
|
+
if m.ID == <%= model.id_field.zero_value_expression %> {
|
119
|
+
return false, nil
|
120
|
+
}
|
118
121
|
g := GoonFromContext(ctx)
|
119
122
|
key, err := g.KeyError(m)
|
120
123
|
if err != nil {
|
@@ -133,8 +136,7 @@ func (s *<%= store_name %>) Exist(ctx context.Context, m *<%= model.name %>) (bo
|
|
133
136
|
}
|
134
137
|
|
135
138
|
func (s *<%= store_name %>) Create(ctx context.Context, m *<%= model.name %>) (*datastore.Key, error) {
|
136
|
-
err := m.PrepareToCreate()
|
137
|
-
if err != nil {
|
139
|
+
if err := m.PrepareToCreate(); err != nil {
|
138
140
|
return nil, err
|
139
141
|
}
|
140
142
|
return s.PutWith(ctx, m, func() error {
|
@@ -144,15 +146,14 @@ func (s *<%= store_name %>) Create(ctx context.Context, m *<%= model.name %>) (*
|
|
144
146
|
}
|
145
147
|
if exist {
|
146
148
|
log.Errorf(ctx, "Failed to create %v because of another entity has same key\n", m)
|
147
|
-
return fmt.Errorf("Duplicate <%= model.
|
149
|
+
return fmt.Errorf("Duplicate <%= model.id_name %> error: %q of %v\n", m.<%= model.id_name %>, m)
|
148
150
|
}
|
149
151
|
return nil
|
150
152
|
})
|
151
153
|
}
|
152
154
|
|
153
155
|
func (s *<%= store_name %>) Update(ctx context.Context, m *<%= model.name %>) (*datastore.Key, error) {
|
154
|
-
err := m.PrepareToUpdate()
|
155
|
-
if err != nil {
|
156
|
+
if err := m.PrepareToUpdate(); err != nil {
|
156
157
|
return nil, err
|
157
158
|
}
|
158
159
|
return s.PutWith(ctx, m, func() error {
|
@@ -162,7 +163,7 @@ func (s *<%= store_name %>) Update(ctx context.Context, m *<%= model.name %>) (*
|
|
162
163
|
}
|
163
164
|
if !exist {
|
164
165
|
log.Errorf(ctx, "Failed to update %v because it doesn't exist\n", m)
|
165
|
-
return fmt.Errorf("No data to update %q of %v\n", m.<%= model.
|
166
|
+
return fmt.Errorf("No data to update %q of %v\n", m.<%= model.id_name %>, m)
|
166
167
|
}
|
167
168
|
return nil
|
168
169
|
})
|
@@ -244,7 +245,11 @@ func (s *<%= store_name %>) ValidateUniqueness(ctx context.Context, m *<%= model
|
|
244
245
|
if err != nil {
|
245
246
|
return err
|
246
247
|
}
|
247
|
-
|
248
|
+
b := 0
|
249
|
+
if m.IsPersisted() {
|
250
|
+
b = 1
|
251
|
+
}
|
252
|
+
if c > b {
|
248
253
|
return &ValidationError{
|
249
254
|
Field: field,
|
250
255
|
Message: fmt.Sprintf("%v has already been taken", value),
|
data/lib/goa_model_gen/type.rb
CHANGED
@@ -123,9 +123,12 @@ module GoaModelGen
|
|
123
123
|
(conv == 'generate') && !fields.empty?
|
124
124
|
end
|
125
125
|
|
126
|
+
def id_field
|
127
|
+
@id_field ||= Field.new(id_name, {'type' => goon['id_type']})
|
128
|
+
end
|
129
|
+
|
126
130
|
def fields_including_id
|
127
131
|
if goon && goon['id_type']
|
128
|
-
id_field = Field.new(id_name, {'type' => goon['id_type']})
|
129
132
|
[id_field] + fields
|
130
133
|
else
|
131
134
|
fields
|
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.6.
|
4
|
+
version: 0.6.1
|
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-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|