go-on-rails 0.1.8 → 0.1.9

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
  SHA1:
3
- metadata.gz: 43f78729f6b807de4949b5bfff9f06dde9854808
4
- data.tar.gz: 40b116bfcb24d9e7a3d378827adc6734ce3fa673
3
+ metadata.gz: 70e8dc6ab9f83a091e415e3024b7b23aebe413eb
4
+ data.tar.gz: a489ffa6df65afdbff314b2c61429007fcb2c886
5
5
  SHA512:
6
- metadata.gz: 5ab347d8b5206dbd0ddd711dbe696ce39f56bf61b5ff8d3d37b31d48edc93931beecc10587f73754a69219008078fb804016adddf6f584e746f658185207311d
7
- data.tar.gz: c095725253a77157d9e19b4327d55ea72f3874243a703fec2ac88b42e5e7225bcf06821dfe9ecd79fbb9983e39b404928157d4f1bc560d725d05585baf066f9f
6
+ metadata.gz: f3112295d0c589078b792d7938786a5c2b25a02965d525821e53072ef42849222c88f49cff51b3afa9fa1928c6907ed369d10d94367fee72253b11b64bcba51c
7
+ data.tar.gz: 2b5b01368af6cb2aa0f37191865e9325e3a36407fbc2e7ffe1645cdff0c1bd9e58e5affc7a4af1503280e8614cc89274a81dd648f71686ee792dd13cb370a45e
data/README.md CHANGED
@@ -5,6 +5,8 @@
5
5
  go-on-rails
6
6
  ====
7
7
 
8
+ [中文文档](./README_zh.md)
9
+
8
10
  go-on-rails is a Rails generator created for three scenarios:
9
11
 
10
12
  1. Integrate some APIs written in Golang to existed Rails app for high performance
@@ -107,12 +109,12 @@ And the gem is still under development, so there're a lot of known issues.
107
109
 
108
110
  ## Golang dependencies by default
109
111
 
110
- * `github.com/jmoiron/sqlx`: an extension on the standard `database/sql` database API library
111
- * `github.com/mattn/go-sqlite3`: a SQLite driver
112
- * `github.com/go-sql-driver/mysql`: a MySQL driver
113
- * `github.com/lib/pq`: a PostgreSQL driver
114
- * `github.com/asaskevich/govalidator`: for the struct validation
115
- * `gopkg.in/gin-gonic/gin.v1`: a HTTP web framework
112
+ * [github.com/jmoiron/sqlx](https://github.com/jmoiron/sqlx): an extension on the standard `database/sql` database API library
113
+ * [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3): a SQLite driver
114
+ * [github.com/go-sql-driver/mysql](https://github.com/go-sql-driver/mysql): a MySQL driver
115
+ * [github.com/lib/pq](https://github.com/lib/pq): a PostgreSQL driver
116
+ * [github.com/asaskevich/govalidator](https://github.com/asaskevich/govalidator): for the struct validation
117
+ * [github.com/gin-gonic/gin](https://github.com/gin-gonic/gin): a HTTP web framework
116
118
 
117
119
  ## Acknowledgement
118
120
 
@@ -1,5 +1,7 @@
1
- <%- param_name_plural = table_name = @model_name.underscore.pluralize -%>
2
- <%- model_name_underscore = @model_name.underscore -%>
1
+ <%#- var_pmn stands for pluralized model name, its format is same as the table name -#%>
2
+ <%- var_pmn = table_name = @model_name.underscore.pluralize -%>
3
+ <%#- var_umn stands for underscored model name -#%>
4
+ <%- var_umn = @model_name.underscore -%>
3
5
  <%- col_names = @struct_info[:col_names] -%>
4
6
  <%- has_assoc = !@struct_info[:assoc_info][:has_many].empty? || !@struct_info[:assoc_info][:has_one].empty? -%>
5
7
  // Package models includes the functions on the model <%= @model_name %>.
@@ -41,7 +43,7 @@ type <%= @model_name %>Page struct {
41
43
  orderStr string
42
44
  }
43
45
 
44
- // Current get the current page of <%= @model_name %>Page object for pagination
46
+ // Current() get the current page of <%= @model_name %>Page object for pagination
45
47
  func (_p *<%= @model_name %>Page) Current() ([]<%= @model_name %>, error) {
46
48
  if _, exist := _p.Order["id"]; !exist {
47
49
  return nil, errors.New("No id order specified in Order map")
@@ -61,17 +63,17 @@ func (_p *<%= @model_name %>Page) Current() ([]<%= @model_name %>, error) {
61
63
  <% end %>
62
64
  whereParams := []interface{}{}
63
65
  whereParams = append(append(whereParams, _p.WhereParams...), idParams...)
64
- <%= param_name_plural %>, err := Find<%= @model_name.pluralize %>Where(whereStr, whereParams...)
66
+ <%= var_pmn %>, err := Find<%= @model_name.pluralize %>Where(whereStr, whereParams...)
65
67
  if err != nil {
66
68
  return nil, err
67
69
  }
68
- if len(<%= param_name_plural %>) != 0 {
69
- _p.FirstId, _p.LastId = <%= param_name_plural %>[0].Id, <%= param_name_plural %>[len(<%= param_name_plural %>)-1].Id
70
+ if len(<%= var_pmn %>) != 0 {
71
+ _p.FirstId, _p.LastId = <%= var_pmn %>[0].Id, <%= var_pmn %>[len(<%= var_pmn %>)-1].Id
70
72
  }
71
- return <%= param_name_plural %>, nil
73
+ return <%= var_pmn %>, nil
72
74
  }
73
75
 
74
- // Current get the previous page of <%= @model_name %>Page object for pagination
76
+ // Previous() get the previous page of <%= @model_name %>Page object for pagination
75
77
  func (_p *<%= @model_name %>Page) Previous() ([]<%= @model_name %>, error) {
76
78
  if _p.PageNum == 0 {
77
79
  return nil, errors.New("This's the first page, no previous page yet")
@@ -94,18 +96,18 @@ func (_p *<%= @model_name %>Page) Previous() ([]<%= @model_name %>, error) {
94
96
  <% end %>
95
97
  whereParams := []interface{}{}
96
98
  whereParams = append(append(whereParams, _p.WhereParams...), idParams...)
97
- <%= param_name_plural %>, err := Find<%= @model_name.pluralize %>Where(whereStr, whereParams...)
99
+ <%= var_pmn %>, err := Find<%= @model_name.pluralize %>Where(whereStr, whereParams...)
98
100
  if err != nil {
99
101
  return nil, err
100
102
  }
101
- if len(<%= param_name_plural %>) != 0 {
102
- _p.FirstId, _p.LastId = <%= param_name_plural %>[0].Id, <%= param_name_plural %>[len(<%= param_name_plural %>)-1].Id
103
+ if len(<%= var_pmn %>) != 0 {
104
+ _p.FirstId, _p.LastId = <%= var_pmn %>[0].Id, <%= var_pmn %>[len(<%= var_pmn %>)-1].Id
103
105
  }
104
106
  _p.PageNum -= 1
105
- return <%= param_name_plural %>, nil
107
+ return <%= var_pmn %>, nil
106
108
  }
107
109
 
108
- // Current get the next page of <%= @model_name %>Page object for pagination
110
+ // Next() get the next page of <%= @model_name %>Page object for pagination
109
111
  func (_p *<%= @model_name %>Page) Next() ([]<%= @model_name %>, error) {
110
112
  if _p.PageNum == _p.TotalPages-1 {
111
113
  return nil, errors.New("This's the last page, no next page yet")
@@ -128,15 +130,31 @@ func (_p *<%= @model_name %>Page) Next() ([]<%= @model_name %>, error) {
128
130
  <% end %>
129
131
  whereParams := []interface{}{}
130
132
  whereParams = append(append(whereParams, _p.WhereParams...), idParams...)
131
- <%= param_name_plural %>, err := Find<%= @model_name.pluralize %>Where(whereStr, whereParams...)
133
+ <%= var_pmn %>, err := Find<%= @model_name.pluralize %>Where(whereStr, whereParams...)
132
134
  if err != nil {
133
135
  return nil, err
134
136
  }
135
- if len(<%= param_name_plural %>) != 0 {
136
- _p.FirstId, _p.LastId = <%= param_name_plural %>[0].Id, <%= param_name_plural %>[len(<%= param_name_plural %>)-1].Id
137
+ if len(<%= var_pmn %>) != 0 {
138
+ _p.FirstId, _p.LastId = <%= var_pmn %>[0].Id, <%= var_pmn %>[len(<%= var_pmn %>)-1].Id
137
139
  }
138
140
  _p.PageNum += 1
139
- return <%= param_name_plural %>, nil
141
+ return <%= var_pmn %>, nil
142
+ }
143
+
144
+ // GetPage() is a helper function for the <%= @model_name %>Page object to return a corresponding page due to
145
+ // the parameter passed in, one of "previous, current or next"
146
+ func (_p *<%= @model_name %>Page) GetPage(direction string) (ps []<%= @model_name %>, err error) {
147
+ switch direction {
148
+ case "previous":
149
+ ps, _ = _p.Previous()
150
+ case "next":
151
+ ps, _ = _p.Next()
152
+ case "current":
153
+ ps, _ = _p.Current()
154
+ default:
155
+ return nil, errors.New("Error: wrong dircetion! None of previous, current or next!")
156
+ }
157
+ return
140
158
  }
141
159
 
142
160
  // buildOrder is for <%= @model_name %>Page object to build SQL ORDER clause
@@ -204,121 +222,121 @@ func (_p *<%= @model_name %>Page) buildPageCount() error {
204
222
  }
205
223
 
206
224
 
207
- // Find<%= @model_name %> find a single <%= model_name_underscore %> by an ID
225
+ // Find<%= @model_name %> find a single <%= var_umn %> by an ID
208
226
  func Find<%= @model_name %>(id int64) (*<%= @model_name %>, error) {
209
227
  if id == 0 {
210
228
  return nil, errors.New("Invalid ID: it can't be zero")
211
229
  }
212
- _<%= model_name_underscore %> := <%= @model_name %>{}
213
- err := DB.Get(&_<%= model_name_underscore %>, DB.Rebind(`SELECT * FROM <%= table_name %> WHERE id = ? LIMIT 1`), id)
230
+ _<%= var_umn %> := <%= @model_name %>{}
231
+ err := DB.Get(&_<%= var_umn %>, DB.Rebind(`SELECT * FROM <%= table_name %> WHERE id = ? LIMIT 1`), id)
214
232
  if err != nil {
215
233
  log.Printf("Error: %v\n", err)
216
234
  return nil, err
217
235
  }
218
- return &_<%= model_name_underscore %>, nil
236
+ return &_<%= var_umn %>, nil
219
237
  }
220
238
 
221
- // First<%= @model_name %> find the first one <%= model_name_underscore %> by ID ASC order
239
+ // First<%= @model_name %> find the first one <%= var_umn %> by ID ASC order
222
240
  func First<%= @model_name %>() (*<%= @model_name %>, error) {
223
- _<%= model_name_underscore %> := <%= @model_name %>{}
224
- err := DB.Get(&_<%= model_name_underscore %>, DB.Rebind(`SELECT * FROM <%= table_name %> ORDER BY id ASC LIMIT 1`))
241
+ _<%= var_umn %> := <%= @model_name %>{}
242
+ err := DB.Get(&_<%= var_umn %>, DB.Rebind(`SELECT * FROM <%= table_name %> ORDER BY id ASC LIMIT 1`))
225
243
  if err != nil {
226
244
  log.Printf("Error: %v\n", err)
227
245
  return nil, err
228
246
  }
229
- return &_<%= model_name_underscore %>, nil
247
+ return &_<%= var_umn %>, nil
230
248
  }
231
249
 
232
- // First<%= @model_name.pluralize %> find the first N <%= model_name_underscore.pluralize %> by ID ASC order
250
+ // First<%= @model_name.pluralize %> find the first N <%= var_umn.pluralize %> by ID ASC order
233
251
  func First<%= @model_name.pluralize %>(n uint32) ([]<%= @model_name %>, error) {
234
- _<%= param_name_plural %> := []<%= @model_name %>{}
252
+ _<%= var_pmn %> := []<%= @model_name %>{}
235
253
  sql := fmt.Sprintf("SELECT * FROM <%= table_name %> ORDER BY id ASC LIMIT %v", n)
236
- err := DB.Select(&_<%= param_name_plural %>, DB.Rebind(sql))
254
+ err := DB.Select(&_<%= var_pmn %>, DB.Rebind(sql))
237
255
  if err != nil {
238
256
  log.Printf("Error: %v\n", err)
239
257
  return nil, err
240
258
  }
241
- return _<%= param_name_plural %>, nil
259
+ return _<%= var_pmn %>, nil
242
260
  }
243
261
 
244
- // Last<%= @model_name %> find the last one <%= model_name_underscore %> by ID DESC order
262
+ // Last<%= @model_name %> find the last one <%= var_umn %> by ID DESC order
245
263
  func Last<%= @model_name %>() (*<%= @model_name %>, error) {
246
- _<%= model_name_underscore %> := <%= @model_name %>{}
247
- err := DB.Get(&_<%= model_name_underscore %>, DB.Rebind(`SELECT * FROM <%= table_name %> ORDER BY id DESC LIMIT 1`))
264
+ _<%= var_umn %> := <%= @model_name %>{}
265
+ err := DB.Get(&_<%= var_umn %>, DB.Rebind(`SELECT * FROM <%= table_name %> ORDER BY id DESC LIMIT 1`))
248
266
  if err != nil {
249
267
  log.Printf("Error: %v\n", err)
250
268
  return nil, err
251
269
  }
252
- return &_<%= model_name_underscore %>, nil
270
+ return &_<%= var_umn %>, nil
253
271
  }
254
272
 
255
- // Last<%= @model_name.pluralize %> find the last N <%= model_name_underscore.pluralize %> by ID DESC order
273
+ // Last<%= @model_name.pluralize %> find the last N <%= var_umn.pluralize %> by ID DESC order
256
274
  func Last<%= @model_name.pluralize %>(n uint32) ([]<%= @model_name %>, error) {
257
- _<%= param_name_plural %> := []<%= @model_name %>{}
275
+ _<%= var_pmn %> := []<%= @model_name %>{}
258
276
  sql := fmt.Sprintf("SELECT * FROM <%= table_name %> ORDER BY id DESC LIMIT %v", n)
259
- err := DB.Select(&_<%= param_name_plural %>, DB.Rebind(sql))
277
+ err := DB.Select(&_<%= var_pmn %>, DB.Rebind(sql))
260
278
  if err != nil {
261
279
  log.Printf("Error: %v\n", err)
262
280
  return nil, err
263
281
  }
264
- return _<%= param_name_plural %>, nil
282
+ return _<%= var_pmn %>, nil
265
283
  }
266
284
 
267
- // Find<%= @model_name.pluralize %> find one or more <%= model_name_underscore.pluralize %> by the given ID(s)
285
+ // Find<%= @model_name.pluralize %> find one or more <%= var_umn.pluralize %> by the given ID(s)
268
286
  func Find<%= @model_name.pluralize %>(ids ...int64) ([]<%= @model_name %>, error) {
269
287
  if len(ids) == 0 {
270
288
  msg := "At least one or more ids needed"
271
289
  log.Println(msg)
272
290
  return nil, errors.New(msg)
273
291
  }
274
- _<%= param_name_plural %> := []<%= @model_name %>{}
292
+ _<%= var_pmn %> := []<%= @model_name %>{}
275
293
  idsHolder := strings.Repeat(",?", len(ids)-1)
276
294
  sql := DB.Rebind(fmt.Sprintf(`SELECT * FROM <%= table_name %> WHERE id IN (?%s)`, idsHolder))
277
295
  idsT := []interface{}{}
278
296
  for _,id := range ids {
279
297
  idsT = append(idsT, interface{}(id))
280
298
  }
281
- err := DB.Select(&_<%= param_name_plural %>, sql, idsT...)
299
+ err := DB.Select(&_<%= var_pmn %>, sql, idsT...)
282
300
  if err != nil {
283
301
  log.Printf("Error: %v\n", err)
284
302
  return nil, err
285
303
  }
286
- return _<%= param_name_plural %>, nil
304
+ return _<%= var_pmn %>, nil
287
305
  }
288
306
 
289
- // Find<%= @model_name %>By find a single <%= model_name_underscore %> by a field name and a value
307
+ // Find<%= @model_name %>By find a single <%= var_umn %> by a field name and a value
290
308
  func Find<%= @model_name %>By(field string, val interface{}) (*<%= @model_name %>, error) {
291
- _<%= model_name_underscore %> := <%= @model_name %>{}
309
+ _<%= var_umn %> := <%= @model_name %>{}
292
310
  sqlFmt := `SELECT * FROM <%= table_name %> WHERE %s = ? LIMIT 1`
293
311
  sqlStr := fmt.Sprintf(sqlFmt, field)
294
- err := DB.Get(&_<%= model_name_underscore %>, DB.Rebind(sqlStr), val)
312
+ err := DB.Get(&_<%= var_umn %>, DB.Rebind(sqlStr), val)
295
313
  if err != nil {
296
314
  log.Printf("Error: %v\n", err)
297
315
  return nil, err
298
316
  }
299
- return &_<%= model_name_underscore %>, nil
317
+ return &_<%= var_umn %>, nil
300
318
  }
301
319
 
302
- // Find<%= @model_name.pluralize %>By find all <%= param_name_plural %> by a field name and a value
303
- func Find<%= @model_name.pluralize %>By(field string, val interface{}) (_<%= param_name_plural %> []<%= @model_name %>, err error) {
320
+ // Find<%= @model_name.pluralize %>By find all <%= var_pmn %> by a field name and a value
321
+ func Find<%= @model_name.pluralize %>By(field string, val interface{}) (_<%= var_pmn %> []<%= @model_name %>, err error) {
304
322
  sqlFmt := `SELECT * FROM <%= table_name %> WHERE %s = ?`
305
323
  sqlStr := fmt.Sprintf(sqlFmt, field)
306
- err = DB.Select(&_<%= param_name_plural %>, DB.Rebind(sqlStr), val)
324
+ err = DB.Select(&_<%= var_pmn %>, DB.Rebind(sqlStr), val)
307
325
  if err != nil {
308
326
  log.Printf("Error: %v\n", err)
309
327
  return nil, err
310
328
  }
311
- return _<%= param_name_plural %>, nil
329
+ return _<%= var_pmn %>, nil
312
330
  }
313
331
 
314
332
  // All<%= @model_name.pluralize %> get all the <%= @model_name %> records
315
- func All<%= @model_name.pluralize %>() (<%= param_name_plural %> []<%= @model_name %>, err error) {
316
- err = DB.Select(&<%= param_name_plural %>, "SELECT * FROM <%= table_name %>")
333
+ func All<%= @model_name.pluralize %>() (<%= var_pmn %> []<%= @model_name %>, err error) {
334
+ err = DB.Select(&<%= var_pmn %>, "SELECT * FROM <%= table_name %>")
317
335
  if err != nil {
318
336
  log.Println(err)
319
337
  return nil, err
320
338
  }
321
- return <%= param_name_plural %>, nil
339
+ return <%= var_pmn %>, nil
322
340
  }
323
341
 
324
342
  // <%= @model_name %>Count get the count of all the <%= @model_name %> records
@@ -351,21 +369,21 @@ func <%= @model_name %>CountWhere(where string, args ...interface{}) (c int64, e
351
369
  }
352
370
 
353
371
  // <%= @model_name %>IncludesWhere get the <%= @model_name %> associated models records, it's just the eager_load function
354
- func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interface{}) (_<%= param_name_plural %> []<%= @model_name %>, err error) {
355
- _<%= param_name_plural %>, err = Find<%= @model_name.pluralize %>Where(sql, args...)
372
+ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interface{}) (_<%= var_pmn %> []<%= @model_name %>, err error) {
373
+ _<%= var_pmn %>, err = Find<%= @model_name.pluralize %>Where(sql, args...)
356
374
  if err != nil {
357
375
  log.Println(err)
358
376
  return nil, err
359
377
  }
360
378
  if len(assocs) == 0 {
361
379
  log.Println("No associated fields ard specified")
362
- return _<%= param_name_plural %>, err
380
+ return _<%= var_pmn %>, err
363
381
  }
364
- if len(_<%= param_name_plural %>) <= 0 {
382
+ if len(_<%= var_pmn %>) <= 0 {
365
383
  return nil, errors.New("No results available")
366
384
  }
367
- ids := make([]interface{}, len(_<%= param_name_plural %>))
368
- for _, v := range _<%= param_name_plural %> {
385
+ ids := make([]interface{}, len(_<%= var_pmn %>))
386
+ for _, v := range _<%= var_pmn %> {
369
387
  ids = append(ids, interface{}(v.Id))
370
388
  }
371
389
  <%- if has_assoc -%>
@@ -378,19 +396,19 @@ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interf
378
396
  case "<%= k.underscore.pluralize %>":
379
397
  <%- if v[:through] || v[:as] -%>
380
398
  // FIXME: optimize the query
381
- for i, vvv := range _<%= param_name_plural %> {
399
+ for i, vvv := range _<%= var_pmn %> {
382
400
  _<%= k.underscore.pluralize %>, err := <%= @model_name %>Get<%= k.pluralize %>(vvv.Id)
383
401
  if err != nil {
384
402
  continue
385
403
  }
386
404
  vvv.<%= k %> = _<%= k.underscore.pluralize %>
387
- _<%= param_name_plural %>[i] = vvv
405
+ _<%= var_pmn %>[i] = vvv
388
406
  }
389
407
  <%- else -%>
390
408
  <%- if v[:foreign_key] -%>
391
409
  where := fmt.Sprintf("<%= v[:foreign_key] %> IN (?%s)", idsHolder)
392
410
  <%- else -%>
393
- where := fmt.Sprintf("<%= model_name_underscore %>_id IN (?%s)", idsHolder)
411
+ where := fmt.Sprintf("<%= var_umn %>_id IN (?%s)", idsHolder)
394
412
  <%- end -%>
395
413
  _<%= v[:class_name].underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>Where(where, ids...)
396
414
  if err != nil {
@@ -398,7 +416,7 @@ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interf
398
416
  continue
399
417
  }
400
418
  for _, vv := range _<%= v[:class_name].underscore.pluralize %> {
401
- for i, vvv := range _<%= param_name_plural %> {
419
+ for i, vvv := range _<%= var_pmn %> {
402
420
  <%- if v[:foreign_key] -%>
403
421
  if vv.<%= v[:foreign_key].to_s.camelize %> == vvv.Id {
404
422
  vvv.<%= k %> = append(vvv.<%= k %>, vv)
@@ -408,7 +426,7 @@ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interf
408
426
  vvv.<%= k %> = append(vvv.<%= k %>, vv)
409
427
  }
410
428
  <%- end -%>
411
- _<%= param_name_plural %>[i].<%= k %> = vvv.<%= k %>
429
+ _<%= var_pmn %>[i].<%= k %> = vvv.<%= k %>
412
430
  }
413
431
  }
414
432
  <%- end -%>
@@ -421,7 +439,7 @@ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interf
421
439
  <%- if v[:foreign_key] -%>
422
440
  where := fmt.Sprintf("<%= v[:foreign_key] %> IN (?%s)", idsHolder)
423
441
  <%- else -%>
424
- where := fmt.Sprintf("<%= model_name_underscore %>_id IN (?%s)", idsHolder)
442
+ where := fmt.Sprintf("<%= var_umn %>_id IN (?%s)", idsHolder)
425
443
  <%- end -%>
426
444
  _<%= v[:class_name].underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>Where(where, ids...)
427
445
  if err != nil {
@@ -429,7 +447,7 @@ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interf
429
447
  continue
430
448
  }
431
449
  for _, vv := range _<%= v[:class_name].underscore.pluralize %> {
432
- for i, vvv := range _<%= param_name_plural %> {
450
+ for i, vvv := range _<%= var_pmn %> {
433
451
  <%- if v[:foreign_key] -%>
434
452
  if vv.<%= v[:foreign_key].to_s.camelize %> == vvv.Id {
435
453
  vvv.<%= k %> = vv
@@ -439,7 +457,7 @@ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interf
439
457
  vvv.<%= k %> = vv
440
458
  }
441
459
  <%- end -%>
442
- _<%= param_name_plural %>[i].<%= k %> = vvv.<%= k %>
460
+ _<%= var_pmn %>[i].<%= k %> = vvv.<%= k %>
443
461
  }
444
462
  }
445
463
  <%- end -%>
@@ -447,7 +465,7 @@ func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interf
447
465
  }
448
466
  }
449
467
  <%- end -%>
450
- return _<%= param_name_plural %>, nil
468
+ return _<%= var_pmn %>, nil
451
469
  }
452
470
 
453
471
  // <%= @model_name %>Ids get all the IDs of <%= @model_name %> records
@@ -507,7 +525,7 @@ func <%= @model_name %>StrCol(col, where string, args ...interface{}) (strColRec
507
525
  // Find<%= @model_name.pluralize %>Where query use a partial SQL clause that usually following after WHERE
508
526
  // with placeholders, eg: FindUsersWhere("first_name = ? AND age > ?", "John", 18)
509
527
  // will return those records in the table "users" whose first_name is "John" and age elder than 18
510
- func Find<%= @model_name.pluralize %>Where(where string, args ...interface{}) (<%= param_name_plural %> []<%= @model_name %>, err error) {
528
+ func Find<%= @model_name.pluralize %>Where(where string, args ...interface{}) (<%= var_pmn %> []<%= @model_name %>, err error) {
511
529
  sql := "SELECT * FROM <%= table_name %>"
512
530
  if len(where) > 0 {
513
531
  sql = sql + " WHERE " + where
@@ -517,12 +535,12 @@ func Find<%= @model_name.pluralize %>Where(where string, args ...interface{}) (<
517
535
  log.Println(err)
518
536
  return nil, err
519
537
  }
520
- err = stmt.Select(&<%= param_name_plural %>, args...)
538
+ err = stmt.Select(&<%= var_pmn %>, args...)
521
539
  if err != nil {
522
540
  log.Println(err)
523
541
  return nil, err
524
542
  }
525
- return <%= param_name_plural %>, nil
543
+ return <%= var_pmn %>, nil
526
544
  }
527
545
 
528
546
  // Find<%= @model_name %>BySql query use a complete SQL clause
@@ -534,30 +552,30 @@ func Find<%= @model_name %>BySql(sql string, args ...interface{}) (*<%= @model_n
534
552
  log.Println(err)
535
553
  return nil, err
536
554
  }
537
- _<%= model_name_underscore %> := &<%= @model_name %>{}
538
- err = stmt.Get(_<%= model_name_underscore %>, args...)
555
+ _<%= var_umn %> := &<%= @model_name %>{}
556
+ err = stmt.Get(_<%= var_umn %>, args...)
539
557
  if err != nil {
540
558
  log.Println(err)
541
559
  return nil, err
542
560
  }
543
- return _<%= model_name_underscore %>, nil
561
+ return _<%= var_umn %>, nil
544
562
  }
545
563
 
546
564
  // Find<%= @model_name.pluralize %>BySql query use a complete SQL clause
547
565
  // with placeholders, eg: FindUsersBySql("SELECT * FROM users WHERE first_name = ? AND age > ?", "John", 18)
548
566
  // will return those records in the table "users" whose first_name is "John" and age elder than 18
549
- func Find<%= @model_name.pluralize %>BySql(sql string, args ...interface{}) (<%= param_name_plural %> []<%= @model_name %>, err error) {
567
+ func Find<%= @model_name.pluralize %>BySql(sql string, args ...interface{}) (<%= var_pmn %> []<%= @model_name %>, err error) {
550
568
  stmt, err := DB.Preparex(DB.Rebind(sql))
551
569
  if err != nil {
552
570
  log.Println(err)
553
571
  return nil, err
554
572
  }
555
- err = stmt.Select(&<%= param_name_plural %>, args...)
573
+ err = stmt.Select(&<%= var_pmn %>, args...)
556
574
  if err != nil {
557
575
  log.Println(err)
558
576
  return nil, err
559
577
  }
560
- return <%= param_name_plural %>, nil
578
+ return <%= var_pmn %>, nil
561
579
  }
562
580
 
563
581
  // Create<%= @model_name %> use a named params to create a single <%= @model_name %> record.
@@ -596,8 +614,8 @@ func Create<%= @model_name %>(am map[string]interface{}) (int64, error) {
596
614
  }
597
615
 
598
616
  // Create is a method for <%= @model_name %> to create a record
599
- func (_<%= model_name_underscore %> *<%= @model_name %>) Create() (int64, error) {
600
- ok, err := govalidator.ValidateStruct(_<%= model_name_underscore %>)
617
+ func (_<%= var_umn %> *<%= @model_name %>) Create() (int64, error) {
618
+ ok, err := govalidator.ValidateStruct(_<%= var_umn %>)
601
619
  if !ok {
602
620
  errMsg := "Validate <%= @model_name %> struct error: Unknown error"
603
621
  if err != nil {
@@ -609,11 +627,11 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) Create() (int64, error)
609
627
  <%- unless @struct_info[:timestamp_cols].empty? -%>
610
628
  t := time.Now()
611
629
  <%- @struct_info[:timestamp_cols].each do |c| -%>
612
- _<%= model_name_underscore %>.<%= c.camelize %> = t
630
+ _<%= var_umn %>.<%= c.camelize %> = t
613
631
  <%- end -%>
614
632
  <%- end -%>
615
633
  sql := `INSERT INTO <%= table_name %> (<%= col_names.join(",") %>) VALUES (:<%= col_names.join(",:") %>)`
616
- result, err := DB.NamedExec(sql, _<%= model_name_underscore %>)
634
+ result, err := DB.NamedExec(sql, _<%= var_umn %>)
617
635
  if err != nil {
618
636
  log.Println(err)
619
637
  return 0, err
@@ -630,23 +648,23 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) Create() (int64, error)
630
648
  <%- has_many = @struct_info[:assoc_info][:has_many] -%>
631
649
  <%- has_many.each do |k, v| -%>
632
650
  // <%= k.pluralize %>Create is used for <%= @model_name %> to create the associated objects <%= k.pluralize %>
633
- func (_<%= model_name_underscore %> *<%= @model_name %>) <%= k.pluralize %>Create(am map[string]interface{}) error {
651
+ func (_<%= var_umn %> *<%= @model_name %>) <%= k.pluralize %>Create(am map[string]interface{}) error {
634
652
  <%- if v[:through] -%>
635
653
  // FIXME: use transaction to create these associated objects
636
654
  <%= v[:class_name].underscore %>Id, err := Create<%= v[:class_name] %>(am)
637
655
  if err != nil {
638
656
  return err
639
657
  }
640
- _, err = Create<%= v[:through].to_s.singularize.camelize %>(map[string]interface{}{"<%= model_name_underscore %>_id": _<%= model_name_underscore %>.Id, "<%= v[:class_name].underscore %>_id": <%= v[:class_name].underscore %>Id})
658
+ _, err = Create<%= v[:through].to_s.singularize.camelize %>(map[string]interface{}{"<%= var_umn %>_id": _<%= var_umn %>.Id, "<%= v[:class_name].underscore %>_id": <%= v[:class_name].underscore %>Id})
641
659
  <%- elsif v[:as] -%>
642
- am["<%= v[:as] %>_id"] = _<%= model_name_underscore %>.Id
660
+ am["<%= v[:as] %>_id"] = _<%= var_umn %>.Id
643
661
  am["<%= v[:as] %>_type"] = "<%= @model_name %>"
644
662
  _, err := Create<%= v[:class_name] %>(am)
645
663
  <%- else -%>
646
664
  <%- if v[:foreign_key] -%>
647
- am["<%= v[:foreign_key] %>"] = _<%= model_name_underscore %>.Id
665
+ am["<%= v[:foreign_key] %>"] = _<%= var_umn %>.Id
648
666
  <%- else -%>
649
- am["<%= model_name_underscore %>_id"] = _<%= model_name_underscore %>.Id
667
+ am["<%= var_umn %>_id"] = _<%= var_umn %>.Id
650
668
  <%- end -%>
651
669
  _, err := Create<%= v[:class_name] %>(am)
652
670
  <%- end -%>
@@ -654,12 +672,12 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) <%= k.pluralize %>Creat
654
672
  }
655
673
 
656
674
  // Get<%= k.pluralize %> is used for <%= @model_name %> to get associated objects <%= k.pluralize %>
657
- // Say you have a <%= @model_name %> object named <%= model_name_underscore %>, when you call <%= model_name_underscore %>.Get<%= k.pluralize %>(),
675
+ // Say you have a <%= @model_name %> object named <%= var_umn %>, when you call <%= var_umn %>.Get<%= k.pluralize %>(),
658
676
  // the object will get the associated <%= k.pluralize %> attributes evaluated in the struct
659
- func (_<%= model_name_underscore %> *<%= @model_name %>) Get<%= k.pluralize %>() error {
660
- _<%= k.underscore.pluralize %>, err := <%= @model_name %>Get<%= k.pluralize %>(_<%= model_name_underscore %>.Id)
677
+ func (_<%= var_umn %> *<%= @model_name %>) Get<%= k.pluralize %>() error {
678
+ _<%= k.underscore.pluralize %>, err := <%= @model_name %>Get<%= k.pluralize %>(_<%= var_umn %>.Id)
661
679
  if err == nil {
662
- _<%= model_name_underscore %>.<%= k %> = _<%= k.underscore.pluralize %>
680
+ _<%= var_umn %>.<%= k %> = _<%= k.underscore.pluralize %>
663
681
  }
664
682
  return err
665
683
  }
@@ -674,7 +692,7 @@ func <%= @model_name %>Get<%= k.pluralize %>(id int64) ([]<%= v[:class_name] %>,
674
692
  FROM <%= target_table %>
675
693
  INNER JOIN <%= through_table %>
676
694
  ON <%= target_table %>.id = <%= through_table %>.<%= v[:class_name].underscore %>_id
677
- WHERE <%= through_table %>.<%= model_name_underscore %>_id = ?`
695
+ WHERE <%= through_table %>.<%= var_umn %>_id = ?`
678
696
  _<%= k.underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>BySql(sql, id)
679
697
  <%- elsif v[:as] -%>
680
698
  where := `<%= v[:as] %>_type = "<%= @model_name %>" AND <%= v[:as] %>_id = ?`
@@ -683,7 +701,7 @@ func <%= @model_name %>Get<%= k.pluralize %>(id int64) ([]<%= v[:class_name] %>,
683
701
  <%- if v[:foreign_key] -%>
684
702
  _<%= k.underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>By("<%= v[:foreign_key] %>", id)
685
703
  <%- else -%>
686
- _<%= k.underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>By("<%= model_name_underscore %>_id", id)
704
+ _<%= k.underscore.pluralize %>, err := Find<%= v[:class_name].pluralize %>By("<%= var_umn %>_id", id)
687
705
  <%- end -%>
688
706
  <%- end -%>
689
707
  return _<%= k.underscore.pluralize %>, err
@@ -696,11 +714,11 @@ func <%= @model_name %>Get<%= k.pluralize %>(id int64) ([]<%= v[:class_name] %>,
696
714
  <%- has_one = @struct_info[:assoc_info][:has_one] -%>
697
715
  <%- has_one.each do |k, v| -%>
698
716
  // Create<%= k %> is a method for the <%= @model_name %> model object to create an associated <%= k %> record
699
- func (_<%= model_name_underscore %> *<%= @model_name %>) Create<%= k %>(am map[string]interface{}) error {
717
+ func (_<%= var_umn %> *<%= @model_name %>) Create<%= k %>(am map[string]interface{}) error {
700
718
  <%- if v[:foreign_key] -%>
701
- am["<%= v[:foreign_key] %>"] = _<%= model_name_underscore %>.Id
719
+ am["<%= v[:foreign_key] %>"] = _<%= var_umn %>.Id
702
720
  <%- else -%>
703
- am["<%= model_name_underscore %>_id"] = _<%= model_name_underscore %>.Id
721
+ am["<%= var_umn %>_id"] = _<%= var_umn %>.Id
704
722
  <%- end -%>
705
723
  _, err := Create<%= v[:class_name] %>(am)
706
724
  return err
@@ -714,11 +732,11 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) Create<%= k %>(am map[s
714
732
  <%-# don't create virtual table for polymorphic model -%>
715
733
  <%- next if v[:polymorphic] -%>
716
734
  // Create<%= k %> is a method for a <%= @model_name %> object to create an associated <%= k %> record
717
- func (_<%= model_name_underscore %> *<%= @model_name %>) Create<%= k %>(am map[string]interface{}) error {
735
+ func (_<%= var_umn %> *<%= @model_name %>) Create<%= k %>(am map[string]interface{}) error {
718
736
  <%- if v[:foreign_key] -%>
719
- am["<%= v[:foreign_key] %>"] = _<%= model_name_underscore %>.Id
737
+ am["<%= v[:foreign_key] %>"] = _<%= var_umn %>.Id
720
738
  <%- else -%>
721
- am["<%= model_name_underscore %>_id"] = _<%= model_name_underscore %>.Id
739
+ am["<%= var_umn %>_id"] = _<%= var_umn %>.Id
722
740
  <%- end -%>
723
741
  _, err := Create<%= v[:class_name] %>(am)
724
742
  return err
@@ -727,11 +745,11 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) Create<%= k %>(am map[s
727
745
  <%- end -%>
728
746
 
729
747
  // Destroy is method used for a <%= @model_name %> object to be destroyed.
730
- func (_<%= model_name_underscore %> *<%= @model_name %>) Destroy() error {
731
- if _<%= model_name_underscore %>.Id == 0 {
748
+ func (_<%= var_umn %> *<%= @model_name %>) Destroy() error {
749
+ if _<%= var_umn %>.Id == 0 {
732
750
  return errors.New("Invalid Id field: it can't be a zero value")
733
751
  }
734
- err := Destroy<%= @model_name %>(_<%= model_name_underscore %>.Id)
752
+ err := Destroy<%= @model_name %>(_<%= var_umn %>.Id)
735
753
  return err
736
754
  }
737
755
 
@@ -833,12 +851,12 @@ func destroy<%= @model_name %>Associations(ids ...int64) {
833
851
  <%- case opts[:dependent] -%>
834
852
  <%- when :destroy, :delete_all -%>
835
853
  <%- if opts[:through] -%>
836
- where := fmt.Sprintf("id IN (SELECT id FROM <%= opts[:through].to_s %> WHERE <%= model_name_underscore %>_id IN (?%s))", idsHolder)
854
+ where := fmt.Sprintf("id IN (SELECT id FROM <%= opts[:through].to_s %> WHERE <%= var_umn %>_id IN (?%s))", idsHolder)
837
855
  _, err = Destroy<%= opts[:class_name].pluralize %>Where(where, idsT...)
838
856
  if err != nil {
839
857
  log.Printf("Destroy associated object %s error: %v\n", "<%= opts[:class_name].pluralize %>", err)
840
858
  }
841
- where = fmt.Sprintf("<%= model_name_underscore %>_id IN (?%s)", idsHolder)
859
+ where = fmt.Sprintf("<%= var_umn %>_id IN (?%s)", idsHolder)
842
860
  _, err = Destroy<%= opts[:through].to_s.pluralize.camelize %>Where(where, idsT...)
843
861
  if err != nil {
844
862
  log.Printf("Destroy associated object %s error: %v\n", "<%= opts[:through].to_s.singularize.camelize %>", err)
@@ -853,7 +871,7 @@ func destroy<%= @model_name %>Associations(ids ...int64) {
853
871
  <%- if opts.key? :foreign_key -%>
854
872
  where := fmt.Sprintf("<%= opts[:foreign_key] %> IN (?%s)", idsHolder)
855
873
  <%- else -%>
856
- where := fmt.Sprintf("<%= model_name_underscore %>_id IN (?%s)", idsHolder)
874
+ where := fmt.Sprintf("<%= var_umn %>_id IN (?%s)", idsHolder)
857
875
  <%- end -%>
858
876
  _, err = Destroy<%= opts[:class_name].pluralize %>Where(where, idsT...)
859
877
  if err != nil {
@@ -872,7 +890,7 @@ func destroy<%= @model_name %>Associations(ids ...int64) {
872
890
  <%- if opts.key? :foreign_key -%>
873
891
  sql := fmt.Sprintf("UPDATE <%= opts[:class_name].underscore.pluralize %> SET <%= opts[:foreign_key] %> = 0 WHERE <%= opts[:foreign_key] %> IN (?%s)", idsHolder)
874
892
  <%- else -%>
875
- sql := fmt.Sprintf("UPDATE <%= opts[:class_name].underscore.pluralize %> SET <%= model_name_underscore %>_id = 0 WHERE <%= model_name_underscore %>_id IN (?%s)", idsHolder)
893
+ sql := fmt.Sprintf("UPDATE <%= opts[:class_name].underscore.pluralize %> SET <%= var_umn %>_id = 0 WHERE <%= var_umn %>_id IN (?%s)", idsHolder)
876
894
  <%- end -%>
877
895
  _, err = Update<%= opts[:class_name].pluralize %>BySql(sql, idsT...)
878
896
  if err != nil {
@@ -889,8 +907,8 @@ func destroy<%= @model_name %>Associations(ids ...int64) {
889
907
 
890
908
  // Save method is used for a <%= @model_name %> object to update an existed record mainly.
891
909
  // If no id provided a new record will be created. A UPSERT action will be implemented further.
892
- func (_<%= model_name_underscore %> *<%= @model_name %>) Save() error {
893
- ok, err := govalidator.ValidateStruct(_<%= model_name_underscore %>)
910
+ func (_<%= var_umn %> *<%= @model_name %>) Save() error {
911
+ ok, err := govalidator.ValidateStruct(_<%= var_umn %>)
894
912
  if !ok {
895
913
  errMsg := "Validate <%= @model_name %> struct error: Unknown error"
896
914
  if err != nil {
@@ -899,17 +917,17 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) Save() error {
899
917
  log.Println(errMsg)
900
918
  return errors.New(errMsg)
901
919
  }
902
- if _<%= model_name_underscore %>.Id == 0 {
903
- _, err = _<%= model_name_underscore %>.Create()
920
+ if _<%= var_umn %>.Id == 0 {
921
+ _, err = _<%= var_umn %>.Create()
904
922
  return err
905
923
  }
906
924
  <%- if @struct_info[:timestamp_cols].include? "updated_at" -%>
907
- _<%= model_name_underscore %>.UpdatedAt = time.Now()
925
+ _<%= var_umn %>.UpdatedAt = time.Now()
908
926
  <%- end -%>
909
927
  sqlFmt := `UPDATE <%= table_name %> SET %s WHERE id = %v`
910
928
  <%- save_col_names = col_names - ["created_at"] -%>
911
- sqlStr := fmt.Sprintf(sqlFmt, "<%= save_col_names.zip(save_col_names).map{|c| c.join(" = :")}.join(", ") %>", _<%= model_name_underscore %>.Id)
912
- _, err = DB.NamedExec(sqlStr, _<%= model_name_underscore %>)
929
+ sqlStr := fmt.Sprintf(sqlFmt, "<%= save_col_names.zip(save_col_names).map{|c| c.join(" = :")}.join(", ") %>", _<%= var_umn %>.Id)
930
+ _, err = DB.NamedExec(sqlStr, _<%= var_umn %>)
913
931
  return err
914
932
  }
915
933
 
@@ -943,29 +961,29 @@ func Update<%= @model_name %>(id int64, am map[string]interface{}) error {
943
961
  }
944
962
 
945
963
  // Update is a method used to update a <%= @model_name %> record with the map[string]interface{} typed key-value parameters.
946
- func (_<%= model_name_underscore %> *<%= @model_name %>) Update(am map[string]interface{}) error {
947
- if _<%= model_name_underscore %>.Id == 0 {
964
+ func (_<%= var_umn %> *<%= @model_name %>) Update(am map[string]interface{}) error {
965
+ if _<%= var_umn %>.Id == 0 {
948
966
  return errors.New("Invalid Id field: it can't be a zero value")
949
967
  }
950
- err := Update<%= @model_name %>(_<%= model_name_underscore %>.Id, am)
968
+ err := Update<%= @model_name %>(_<%= var_umn %>.Id, am)
951
969
  return err
952
970
  }
953
971
 
954
972
  // UpdateAttributes method is supposed to be used to update <%= @model_name %> records as corresponding update_attributes in Ruby on Rails.
955
- func (_<%= model_name_underscore %> *<%= @model_name %>) UpdateAttributes(am map[string]interface{}) error {
956
- if _<%= model_name_underscore %>.Id == 0 {
973
+ func (_<%= var_umn %> *<%= @model_name %>) UpdateAttributes(am map[string]interface{}) error {
974
+ if _<%= var_umn %>.Id == 0 {
957
975
  return errors.New("Invalid Id field: it can't be a zero value")
958
976
  }
959
- err := Update<%= @model_name %>(_<%= model_name_underscore %>.Id, am)
977
+ err := Update<%= @model_name %>(_<%= var_umn %>.Id, am)
960
978
  return err
961
979
  }
962
980
 
963
981
  // UpdateColumns method is supposed to be used to update <%= @model_name %> records as corresponding update_columns in Ruby on Rails.
964
- func (_<%= model_name_underscore %> *<%= @model_name %>) UpdateColumns(am map[string]interface{}) error {
965
- if _<%= model_name_underscore %>.Id == 0 {
982
+ func (_<%= var_umn %> *<%= @model_name %>) UpdateColumns(am map[string]interface{}) error {
983
+ if _<%= var_umn %>.Id == 0 {
966
984
  return errors.New("Invalid Id field: it can't be a zero value")
967
985
  }
968
- err := Update<%= @model_name %>(_<%= model_name_underscore %>.Id, am)
986
+ err := Update<%= @model_name %>(_<%= var_umn %>.Id, am)
969
987
  return err
970
988
  }
971
989
 
@@ -4,7 +4,7 @@ import (
4
4
  "flag"
5
5
 
6
6
  c "./controllers"
7
- "gopkg.in/gin-gonic/gin.v1"
7
+ "github.com/gin-gonic/gin"
8
8
  )
9
9
 
10
10
  func main() {
data/lib/tasks/gor.rake CHANGED
@@ -4,7 +4,7 @@ namespace :gor do
4
4
  puts 'Beginning to install Go deps...'
5
5
  system "go get \
6
6
  github.com/jmoiron/sqlx \
7
- gopkg.in/gin-gonic/gin.v1 \
7
+ github.com/gin-gonic/gin \
8
8
  github.com/mattn/go-sqlite3 \
9
9
  github.com/go-sql-driver/mysql \
10
10
  github.com/lib/pq \
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: go-on-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - B1nj0y
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-18 00:00:00.000000000 Z
11
+ date: 2017-10-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Modeling, developing and testing your Golang app with your familiar Rails
14
14
  tools like rails generate, db migration, console etc. It is more meant to help integrating
@@ -57,5 +57,5 @@ rubyforge_project:
57
57
  rubygems_version: 2.5.2
58
58
  signing_key:
59
59
  specification_version: 4
60
- summary: Use Rails generator to Generate a Golang application
60
+ summary: Use Rails to Develop or Generate a Golang application
61
61
  test_files: []