goa_model_gen 0.6.0 → 0.6.1

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: 82c7930eb91dbfa049e61b483ab4e570e5319020a1a0e283bf79c9014b7059f3
4
- data.tar.gz: b7dcdd494bbfaf4f93333398c285f94b31e47d24b2c5adb643cfadb844b2c29d
3
+ metadata.gz: fb100e41bfae1d78b4c222638a59945e42efa13af022c000d55efa3be6d71478
4
+ data.tar.gz: 53d689f9969dda1338ef1d177e56f1da34d04c40e55433e8e1816f8b8fea74de
5
5
  SHA512:
6
- metadata.gz: 873ab6ec5df1fd224c8c07d47ca1b4d77220f9aa16cb67913fede6726f4097bd8dc2682afbff38bc6dfbb8a8ce1b5fc8a637066b67233dc9850ff3f7fc246f4e
7
- data.tar.gz: 682549fb13dc19eb00d4c7a82be17b8ede6a6e1e8cf7c081ccff9dd52883923b2fe4e7cd6524c0c72a77b7e9ae7b3096b901387805bbcbaf2e413212733cee55
6
+ metadata.gz: c93f6c04d0fd3f4a1b72856e19c9a8ad026c2253cd14e9632729c075d0e70277f62464d03532cf90a2bd88326d2a4f9df528c3cfce923a46d8a4bcb5d2d12882
7
+ data.tar.gz: a163ecc4e61e8b03aa8de557a12c029b71a7e8253d910fe45df8c248c9caf4db457c1805a008c5d2bfa6bc870608f9833d047142aacccde45bdd6d9027fdc959
@@ -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 :dryrun, type: :boolean, aliases: 'd', desc: "Don't write or overwrite file"
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.dryrun = options[:dryrun]
89
+ g.skip = options[:skip]
90
+ g.keep_editable = options[:keep_editable]
86
91
  end
87
92
  end
88
93
 
@@ -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, :dryrun
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
- @dryrun = false
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
- generate: "\e[32m", # green # !file_exist
79
- no_change: "\e[37m", # white # file_exist && !modified
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
- modified = already_exist ? (content != File.read(output_path)) : true
91
- action =
92
- !already_exist ? :generate :
93
- !modified ? :no_change :
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)
@@ -43,6 +43,10 @@ func IntToInt64Pointer(v int) *int64 {
43
43
  return &r
44
44
  }
45
45
 
46
+ func StringToStringPointer(v string) *string {
47
+ return &v
48
+ }
49
+
46
50
  func StringPointerToString(v *string) string {
47
51
  return StringPointerToStringWith(v, "")
48
52
  }
@@ -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.goon['id_name'] %> error: %q of %v\n", m.<%= model.goon['id_name'] %>, m)
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.goon['id_name'] %>, m)
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
- if c > 0 {
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),
@@ -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
@@ -1,3 +1,3 @@
1
1
  module GoaModelGen
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  end
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.0
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-07 00:00:00.000000000 Z
11
+ date: 2018-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor