go-on-rails 0.0.9 → 0.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ea55ef1523d312ab11f84c0caeb0fd5b8e9f37f
4
- data.tar.gz: 8f35e3d8a97a4c5f969b528c6b3da4478e33da3a
3
+ metadata.gz: 98efb6c9f89545a36de83af4b3a972af42dcdab3
4
+ data.tar.gz: 15f4301f4f7030325d8e9e29140b31308118e75d
5
5
  SHA512:
6
- metadata.gz: fc3f84d037c2f9fa9b63df087539a414a6f8ee1fd9846832b9a6de103c7a0d16ac0306f885a48350a8a5569f1c71ecc80021864dbc4560c98b9bf87a44b8d3bc
7
- data.tar.gz: 22da36278793791b9cc4518e33ac3f01a81ca76a33570e264df5b1d07e4813ff964b8f68c30b7b58a34c57d9ec0ba1163e35c0e303863fba6241b31f4810429a
6
+ metadata.gz: f3827a7e075ce518bfc57a595174d8b2d6a68372c5f8b093d45b67fae06ad7ab46b20a8e6aa2f10a2a4879da1d62b2fe4312d20dc507060c2bef5c28807770a7
7
+ data.tar.gz: 65b16cacd2909f45a721903be8e51676043c365840db2c82c6a1e44dd18ed85a991827c8fcb1fdf9a31810ca99746ff0068c5037e7f8df9e535b15fa0cbef3b4
data/README.md CHANGED
@@ -9,7 +9,7 @@ go-on-rails aims at three scenarios:
9
9
  2. Use your farmiliar Rails tools to develope and manage a Golang app project
10
10
  3. Convert a *not very complicated* Rails app to Golang equivalent
11
11
 
12
- One or more examples will be given later on.
12
+ Here's a simple [example(tutorial)](https://github.com/goonr/example_simple) shows the basic usage of go-on-rails generator, and some real-world examples are coming soon.
13
13
 
14
14
  ## Prerequisites
15
15
 
@@ -21,7 +21,7 @@ One or more examples will be given later on.
21
21
  Add this line to your application's Gemfile:
22
22
 
23
23
  ```ruby
24
- gem 'go-on-rails', '~> 0.0.9'
24
+ gem 'go-on-rails', '~> 0.1.0'
25
25
  ```
26
26
 
27
27
  And then execute:
@@ -51,7 +51,7 @@ Install the dependent Golang packages:
51
51
  rails gor:deps
52
52
  ```
53
53
 
54
- Then change to the "go_app" directory and run:
54
+ Then change to the `go_app` directory and run:
55
55
 
56
56
  ```bash
57
57
  go run main.go
@@ -65,6 +65,13 @@ More command details about go-on-rails generator:
65
65
  rails g gor --help
66
66
  ```
67
67
 
68
+ ## What will be generated?
69
+
70
+ * Go project directory layout (all under the `go_app` directory, like `views`, `controllers`, `public`)
71
+ * A Go data struct corresponding to each activerecord model
72
+ * And each struct related CRUD functions/methods like FindModel, UpdateModel, DestroyModle etc. All these models related program files under the `go_app/models` directory
73
+ * We use [Gin](https://github.com/gin-gonic/gin) as the default web framework, but you can change it to anyone that you favor in `main.go` and `controllers` programs
74
+
68
75
  And the gem is still under development, so there're a lot of known issues.
69
76
 
70
77
  ## Known issues and TODOs
@@ -79,6 +86,15 @@ And the gem is still under development, so there're a lot of known issues.
79
86
 
80
87
  Really a lot...
81
88
 
89
+ ## Golang dependencies by default
90
+
91
+ * `github.com/jmoiron/sqlx`: an extension on the standard `database/sql` database API library
92
+ * `github.com/mattn/go-sqlite3`: a SQLite driver
93
+ * `github.com/go-sql-driver/mysql`: a MySQL driver
94
+ * `github.com/lib/pq`: a PostgreSQL driver
95
+ * `github.com/asaskevich/govalidator`: for the struct validation
96
+ * `gopkg.in/gin-gonic/gin.v1`: a HTTP web framework
97
+
82
98
  ## Acknowledgement
83
99
 
84
100
  When I had the idea to convert Rails app or build Golang app with Rails tools, I searched github and found the project: https://github.com/t-k/ar2gostruct. And from ar2gostruct I copied some codes on handling data structure convertion and models association, it make my idea come true faster than I imagined.
@@ -133,7 +133,7 @@ func Find<%= @model_name.pluralize %>By(field string, val interface{}) (var_<%=
133
133
 
134
134
  // All<%= @model_name.pluralize %> get all the <%= @model_name %> records
135
135
  func All<%= @model_name.pluralize %>() (<%= param_name_plural %> []<%= @model_name %>, err error) {
136
- err = db.Select(&<%= table_name %>, "SELECT * FROM <%= table_name %>")
136
+ err = db.Select(&<%= param_name_plural %>, "SELECT * FROM <%= table_name %>")
137
137
  if err != nil {
138
138
  log.Println(err)
139
139
  return nil, err
@@ -141,6 +141,36 @@ func All<%= @model_name.pluralize %>() (<%= param_name_plural %> []<%= @model_na
141
141
  return <%= param_name_plural %>, nil
142
142
  }
143
143
 
144
+ // <%= @model_name %>Count get the count of all the <%= @model_name %> records
145
+ func <%= @model_name %>Count() (c int64, err error) {
146
+ err = db.Get(&c, "SELECT count(*) FROM <%= table_name %>")
147
+ if err != nil {
148
+ log.Println(err)
149
+ return 0, err
150
+ }
151
+ return c, nil
152
+ }
153
+
154
+ // <%= @model_name %>CountWhere get the count of all the <%= @model_name %> records with a where clause
155
+ func <%= @model_name %>CountWhere(where string, args ...interface{}) (c int64, err error) {
156
+ sql := "SELECT count(*) FROM <%= table_name %>"
157
+ if len(where) > 0 {
158
+ sql = sql + " WHERE " + where
159
+ }
160
+ stmt, err := db.Preparex(db.Rebind(sql))
161
+ if err != nil {
162
+ log.Println(err)
163
+ return 0, err
164
+ }
165
+ err = stmt.Get(&c, args...)
166
+ if err != nil {
167
+ log.Println(err)
168
+ return 0, err
169
+ }
170
+ return c, nil
171
+ }
172
+
173
+ // <%= @model_name %>IncludesWhere get the <%= @model_name %> associated models records, it's just the eager_load function
144
174
  func <%= @model_name %>IncludesWhere(assocs []string, sql string, args ...interface{}) (var_<%= param_name_plural %> []<%= @model_name %>, err error) {
145
175
  var_<%= param_name_plural %>, err = Find<%= @model_name.pluralize %>Where(sql, args...)
146
176
  if err != nil {
@@ -365,7 +395,7 @@ func Create<%= @model_name %>(am map[string]interface{}) (int64, error) {
365
395
  return lastId, nil
366
396
  }
367
397
 
368
- func (var_<%= model_name_underscore %> *<%= @model_name %>) Create() error {
398
+ func (var_<%= model_name_underscore %> *<%= @model_name %>) Create() (int64, error) {
369
399
  ok, err := govalidator.ValidateStruct(var_<%= model_name_underscore %>)
370
400
  if !ok {
371
401
  errMsg := "Validate <%= @model_name %> struct error: Unknown error"
@@ -373,7 +403,7 @@ func (var_<%= model_name_underscore %> *<%= @model_name %>) Create() error {
373
403
  errMsg = "Validate <%= @model_name %> struct error: " + err.Error()
374
404
  }
375
405
  log.Println(errMsg)
376
- return errors.New(errMsg)
406
+ return 0, errors.New(errMsg)
377
407
  }
378
408
  <%- unless @struct_info[:timestamp_cols].empty? -%>
379
409
  t := time.Now()
@@ -382,8 +412,17 @@ func (var_<%= model_name_underscore %> *<%= @model_name %>) Create() error {
382
412
  <%- end -%>
383
413
  <%- end -%>
384
414
  sql := `INSERT INTO <%= table_name %> (<%= col_names.join(",") %>) VALUES (:<%= col_names.join(",:") %>)`
385
- _, err = db.NamedExec(sql, var_<%= model_name_underscore %>)
386
- return err
415
+ result, err := db.NamedExec(sql, var_<%= model_name_underscore %>)
416
+ if err != nil {
417
+ log.Println(err)
418
+ return 0, err
419
+ }
420
+ lastId, err := result.LastInsertId()
421
+ if err != nil {
422
+ log.Println(err)
423
+ return 0, err
424
+ }
425
+ return lastId, nil
387
426
  }
388
427
 
389
428
  <%- unless @struct_info[:assoc_info][:has_many].empty? -%>
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.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - B1nj0y
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-10 00:00:00.000000000 Z
11
+ date: 2017-05-21 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