go-on-rails 0.1.3 → 0.1.4

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: 1a79eacecb3518a5d6a275b966c46c772af15a74
4
- data.tar.gz: 631f955570f113bb767e83809287f5c39cc8433c
3
+ metadata.gz: 6d2c69a6ebce31c2804821e2984af576d8a45212
4
+ data.tar.gz: fcd5211c4a7eb06c15f8fb82ee37cb71f36ade55
5
5
  SHA512:
6
- metadata.gz: b131b9b03ecca48ba7081d7bd8e27e9228c6c7effaff59e5c0d4defb1634b5c3824d45366bf20fe7cb7ff179ef69bd8906686d19ccdb585054efbc909dbc1efc
7
- data.tar.gz: 7a93441d049b69b48288561088ebebeca048e96ccc309c7e868a656df1fb051556722e982e4b90ee677fa02e965f7f3106a98517be78ee09d80499c571ee752b
6
+ metadata.gz: 7706dd9487899a009c937f4cc114c197e839ffc8a3f89b115de3f59b3301013030b6950207c796b6c236870f9fdfa13a2ae4360663a6db33d27f0c73c533b93a
7
+ data.tar.gz: 0b60b8ded7ee6f87c8533c0cae640dcc07cb1724ee24ec98caed7c2f85e8407ed71a35eedbe3db02170598a729b472e2d4a1b8cef12116e830b4b1578fa24662
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  go-on-rails
4
4
  ====
5
5
 
6
- go-on-rails aims at three scenarios:
6
+ go-on-rails is a Rails generator created for three scenarios:
7
7
 
8
8
  1. Integrate some APIs written in Golang to existed Rails app for high performance
9
9
  2. Use your farmiliar Rails tools to develope and manage a Golang app project
@@ -37,7 +37,7 @@ $ gem install go-on-rails
37
37
 
38
38
  You must have a Rails app or to create one before you can try go-on-rails generator to convert a Rails app to Golang codes.
39
39
 
40
- You can just run the command to convert the application:
40
+ After that you can just run the command to convert the application:
41
41
 
42
42
  ```bash
43
43
  rails g gor [dev(elopment) | pro(duction) | test] [-m model_a model_b model_c ...]
@@ -70,8 +70,17 @@ rails g gor --help
70
70
  * Go project directory layout (all under the `go_app` directory, like `views`, `controllers`, `public`)
71
71
  * A Go data struct corresponding to each activerecord model
72
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
+ * And godoc files for all the functions under `go_app/models/doc`
73
74
  * 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
75
 
76
+ ### View the godoc of all functions
77
+
78
+ You can view the godoc page of all functions in http://localhost:7979/doc/models.html after run:
79
+
80
+ ```bash
81
+ rails gor:doc
82
+ ```
83
+
75
84
  And the gem is still under development, so there're a lot of known issues.
76
85
 
77
86
  ## Known issues and TODOs
@@ -120,7 +129,14 @@ The `dev` branch has a whole Rails environment for development: models, seeds fo
120
129
 
121
130
  ### Testing
122
131
 
123
- Run `rails db:seed` to use the data defined in `db/seeds.rb`. And change to `go_app/models` directory to run `go test` to test generated models-related functions.
132
+ We create four models for testing:
133
+
134
+ - Physician
135
+ - Patient
136
+ - Appointment
137
+ - Picture
138
+
139
+ Run `rails db:seed` to use the data defined in `db/seeds.rb`. And change to `go_app/models` directory to run `go test` to test generated models-related functions. The test covers a small part of the functions currently. More will be added later on.
124
140
 
125
141
  ## License
126
142
 
@@ -60,6 +60,9 @@ class GorGenerator < Rails::Generators::Base
60
60
 
61
61
  # use gofmt to prettify the generated Golang files
62
62
  gofmt_go_files
63
+
64
+ # generate go docs for models
65
+ generate_go_docs
63
66
  end
64
67
 
65
68
  private
@@ -96,6 +99,14 @@ class GorGenerator < Rails::Generators::Base
96
99
  go_files = Rails.root.join('go_app', 'models/*.go').to_s
97
100
  system "gofmt -w #{go_files} > /dev/null 2>&1"
98
101
  end
102
+
103
+ def generate_go_docs
104
+ models_dir = Rails.root.join('go_app', 'models').to_s
105
+ return unless Dir.exist?(File.expand_path(models_dir))
106
+ doc_dir = File.join(models_dir, "doc")
107
+ Dir.mkdir(doc_dir) unless Dir.exist?(doc_dir)
108
+ system "godoc -html #{models_dir} | awk '{ gsub(\"/src/target\", \"\"); print }' > #{doc_dir}/models.html"
109
+ end
99
110
  end
100
111
 
101
112
  require_relative 'go-on-rails/converter'
@@ -2,10 +2,7 @@
2
2
  <%- model_name_underscore = @model_name.underscore -%>
3
3
  <%- col_names = @struct_info[:col_names] -%>
4
4
  <%- has_assoc = !@struct_info[:assoc_info][:has_many].empty? || !@struct_info[:assoc_info][:has_one].empty? -%>
5
- // Package models is generated by go-on-rails, a Rails generator gem:
6
- // https://rubygems.org/gems/go-on-rails
7
- // Or on Github: https://github.com/goonr/go-on-rails
8
- // By B1nj0y <idegorepl@gmail.com>
5
+ // Package models includes the functions on the model <%= @model_name %>.
9
6
  package models
10
7
 
11
8
  import (
@@ -20,6 +17,11 @@ import (
20
17
  "github.com/asaskevich/govalidator"
21
18
  )
22
19
 
20
+ // set flags to output more detailed log
21
+ func init() {
22
+ log.SetFlags(log.LstdFlags | log.Lshortfile)
23
+ }
24
+
23
25
  type <%= @model_name %> struct {
24
26
  <%= @struct_info[:struct_body] -%>
25
27
  }
@@ -380,6 +382,8 @@ func Find<%= @model_name.pluralize %>BySql(sql string, args ...interface{}) (<%=
380
382
  return <%= param_name_plural %>, nil
381
383
  }
382
384
 
385
+ // Create<%= @model_name %> use a named params to create a single <%= @model_name %> record.
386
+ // A named params is key-value map like map[string]interface{}{"first_name": "John", "age": 23} .
383
387
  func Create<%= @model_name %>(am map[string]interface{}) (int64, error) {
384
388
  if len(am) == 0 {
385
389
  return 0, fmt.Errorf("Zero key in the attributes map!")
@@ -413,6 +417,7 @@ func Create<%= @model_name %>(am map[string]interface{}) (int64, error) {
413
417
  return lastId, nil
414
418
  }
415
419
 
420
+ // Create is a method for <%= @model_name %> to create a record
416
421
  func (_<%= model_name_underscore %> *<%= @model_name %>) Create() (int64, error) {
417
422
  ok, err := govalidator.ValidateStruct(_<%= model_name_underscore %>)
418
423
  if !ok {
@@ -446,7 +451,7 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) Create() (int64, error)
446
451
  <%- unless @struct_info[:assoc_info][:has_many].empty? -%>
447
452
  <%- has_many = @struct_info[:assoc_info][:has_many] -%>
448
453
  <%- has_many.each do |k, v| -%>
449
- // <%= k.pluralize %>Create used to create the associated objects
454
+ // <%= k.pluralize %>Create is used for <%= @model_name %> to create the associated objects <%= k.pluralize %>
450
455
  func (_<%= model_name_underscore %> *<%= @model_name %>) <%= k.pluralize %>Create(am map[string]interface{}) error {
451
456
  <%- if v[:through] -%>
452
457
  // FIXME: use transaction to create these associated objects
@@ -470,7 +475,9 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) <%= k.pluralize %>Creat
470
475
  return err
471
476
  }
472
477
 
473
- // Get<%= k.pluralize %> used to get associated objects
478
+ // Get<%= k.pluralize %> is used for <%= @model_name %> to get associated objects <%= k.pluralize %>
479
+ // Say you have a <%= @model_name %> object named <%= model_name_underscore %>, when you call <%= model_name_underscore %>.Get<%= k.pluralize %>(),
480
+ // the object will get the associated <%= k.pluralize %> attributes evaluated in the struct
474
481
  func (_<%= model_name_underscore %> *<%= @model_name %>) Get<%= k.pluralize %>() error {
475
482
  _<%= k.underscore.pluralize %>, err := <%= @model_name %>Get<%= k.pluralize %>(_<%= model_name_underscore %>.Id)
476
483
  if err == nil {
@@ -510,6 +517,7 @@ func <%= @model_name %>Get<%= k.pluralize %>(id int64) ([]<%= v[:class_name] %>,
510
517
  <%- unless @struct_info[:assoc_info][:has_one].empty? -%>
511
518
  <%- has_one = @struct_info[:assoc_info][:has_one] -%>
512
519
  <%- has_one.each do |k, v| -%>
520
+ // Create<%= k %> is a method used for <%= @model_name %> model
513
521
  func (_<%= model_name_underscore %> *<%= @model_name %>) Create<%= k %>(am map[string]interface{}) error {
514
522
  <%- if v[:foreign_key] -%>
515
523
  am["<%= v[:foreign_key] %>"] = _<%= model_name_underscore %>.Id
@@ -527,6 +535,7 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) Create<%= k %>(am map[s
527
535
  <%- belongs_to.each do |k, v| -%>
528
536
  <%-# don't create virtual table for polymorphic model -%>
529
537
  <%- next if v[:polymorphic] -%>
538
+ // Create<%= k %> is a method for a <%= @model_name %> object to create an associated <%= k %> record
530
539
  func (_<%= model_name_underscore %> *<%= @model_name %>) Create<%= k %>(am map[string]interface{}) error {
531
540
  <%- if v[:foreign_key] -%>
532
541
  am["<%= v[:foreign_key] %>"] = _<%= model_name_underscore %>.Id
@@ -539,6 +548,7 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) Create<%= k %>(am map[s
539
548
  <%- end -%>
540
549
  <%- end -%>
541
550
 
551
+ // Destroy is method used for a <%= @model_name %> object to be destroyed.
542
552
  func (_<%= model_name_underscore %> *<%= @model_name %>) Destroy() error {
543
553
  if _<%= model_name_underscore %>.Id == 0 {
544
554
  return errors.New("Invalid Id field: it can't be a zero value")
@@ -547,6 +557,7 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) Destroy() error {
547
557
  return err
548
558
  }
549
559
 
560
+ // Destroy<%= @model_name %> will destroy a <%= @model_name %> record specified by id parameter.
550
561
  func Destroy<%= @model_name %>(id int64) error {
551
562
  <%- if @struct_info[:has_assoc_dependent] -%>
552
563
  // Destroy association objects at first
@@ -561,6 +572,7 @@ func Destroy<%= @model_name %>(id int64) error {
561
572
  return nil
562
573
  }
563
574
 
575
+ // Destroy<%= @model_name.pluralize %> will destroy <%= @model_name %> records those specified by the ids parameters.
564
576
  func Destroy<%= @model_name.pluralize %>(ids ...int64) (int64, error) {
565
577
  if len(ids) == 0 {
566
578
  msg := "At least one or more ids needed"
@@ -621,7 +633,8 @@ func Destroy<%= @model_name.pluralize %>Where(where string, args ...interface{})
621
633
  }
622
634
 
623
635
  <%- if @struct_info[:has_assoc_dependent] -%>
624
- // the func not return err temporarily
636
+ // destroy<%= @model_name %>Associations is a private function used to destroy a <%= @model_name %> record's associated objects.
637
+ // The func not return err temporarily.
625
638
  func destroy<%= @model_name %>Associations(ids ...int64) {
626
639
  idsHolder := ""
627
640
  if len(ids) > 1 {
@@ -696,6 +709,8 @@ func destroy<%= @model_name %>Associations(ids ...int64) {
696
709
  }
697
710
  <%- end -%>
698
711
 
712
+ // Save method is used for a <%= @model_name %> object to update an existed record mainly.
713
+ // If no id provided a new record will be created. A UPSERT action will be implemented further.
699
714
  func (_<%= model_name_underscore %> *<%= @model_name %>) Save() error {
700
715
  ok, err := govalidator.ValidateStruct(_<%= model_name_underscore %>)
701
716
  if !ok {
@@ -707,7 +722,8 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) Save() error {
707
722
  return errors.New(errMsg)
708
723
  }
709
724
  if _<%= model_name_underscore %>.Id == 0 {
710
- return errors.New("Invalid Id field: it can't be a zero value")
725
+ _, err = _<%= model_name_underscore %>.Create()
726
+ return err
711
727
  }
712
728
  <%- if @struct_info[:timestamp_cols].include? "updated_at" -%>
713
729
  _<%= model_name_underscore %>.UpdatedAt = time.Now()
@@ -719,6 +735,7 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) Save() error {
719
735
  return err
720
736
  }
721
737
 
738
+ // Update<%= @model_name %> is used to update a record with a id and map[string]interface{} typed key-value parameters.
722
739
  func Update<%= @model_name %>(id int64, am map[string]interface{}) error {
723
740
  if len(am) == 0 {
724
741
  return errors.New("Zero key in the attributes map!")
@@ -747,6 +764,7 @@ func Update<%= @model_name %>(id int64, am map[string]interface{}) error {
747
764
  return nil
748
765
  }
749
766
 
767
+ // Update is a method used to update a <%= @model_name %> record with the map[string]interface{} typed key-value parameters.
750
768
  func (_<%= model_name_underscore %> *<%= @model_name %>) Update(am map[string]interface{}) error {
751
769
  if _<%= model_name_underscore %>.Id == 0 {
752
770
  return errors.New("Invalid Id field: it can't be a zero value")
@@ -763,6 +781,7 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) UpdateAttributes(am map
763
781
  return err
764
782
  }
765
783
 
784
+ // UpdateColumns method is supposed to be used to update <%= @model_name %> records as corresponding update_columns in Rails
766
785
  func (_<%= model_name_underscore %> *<%= @model_name %>) UpdateColumns(am map[string]interface{}) error {
767
786
  if _<%= model_name_underscore %>.Id == 0 {
768
787
  return errors.New("Invalid Id field: it can't be a zero value")
@@ -771,6 +790,8 @@ func (_<%= model_name_underscore %> *<%= @model_name %>) UpdateColumns(am map[st
771
790
  return err
772
791
  }
773
792
 
793
+ // Update<%= @model_name.pluralize %>BySql is used to update <%= @model_name %> records by a SQL clause
794
+ // that use '?' binding syntax
774
795
  func Update<%= @model_name.pluralize %>BySql(sql string, args ...interface{}) (int64, error) {
775
796
  if sql == "" {
776
797
  return 0, errors.New("A blank SQL clause")
data/lib/tasks/gor.rake CHANGED
@@ -17,4 +17,17 @@ namespace :gor do
17
17
  go_files = Rails.root.join('go_app', 'models/*.go').to_s
18
18
  system "gofmt -w #{go_files} > /dev/null 2>&1"
19
19
  end
20
+
21
+ desc 'View the doc of all the functions generated on models'
22
+ task :doc do
23
+ models_dir = Rails.root.join('go_app', 'models').to_s
24
+ puts 'Please open "http://localhost:7979/doc/models.html" to view the doc of all functions generated on models.'
25
+ puts 'Use Ctrl-C to terminate this server!'
26
+ if RUBY_PLATFORM =~ /darwin/
27
+ system 'open http://localhost:7979/doc/models.html'
28
+ elsif RUBY_PLATFORM =~ /cygwin|mswin|mingw|bccwin|wince|emx/
29
+ system 'start http://localhost:7979/doc/models.html'
30
+ end
31
+ system "godoc -goroot #{models_dir} -http=:7979"
32
+ end
20
33
  end
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.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - B1nj0y
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-27 00:00:00.000000000 Z
11
+ date: 2017-08-07 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