go-on-rails 0.0.7 → 0.0.8
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/README.md +3 -2
- data/lib/generators/gor/go-on-rails/association.rb +1 -1
- data/lib/generators/gor/go-on-rails/converter.rb +6 -4
- data/lib/generators/gor/go-on-rails/validator.rb +60 -0
- data/lib/generators/gor/templates/gor_model.go.erb +22 -2
- data/lib/tasks/gor.rake +2 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca05e99e94cb8cfda6430e5c757669ebad458d58
|
4
|
+
data.tar.gz: 5f2b5a426d3f4c36263edb3d98d79d4c7d94f7e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d2874934773f3139d68ed4cccbde2c22631be8e2048541b3b702687f73bebdcb655ba493d420069df61da34becdb9f6b2fab2cfd97a5be402fe5ccef2e0afbc
|
7
|
+
data.tar.gz: 0c05600084919d689bef80208d4582323ca8e3cb141cfc7d4e3645c9071ff8aba357b57bb14fcda7f6fa2c01b03dfb31313fd331d05203c5b45dd803f2cf8722
|
data/README.md
CHANGED
@@ -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.
|
24
|
+
gem 'go-on-rails', '~> 0.0.8'
|
25
25
|
```
|
26
26
|
|
27
27
|
And then execute:
|
@@ -72,6 +72,7 @@ And the gem is still under development, so there're a lot of known issues.
|
|
72
72
|
* the generated Golang codes includes the association between models, basic relations like has_many, has_one, belongs_to have been supported
|
73
73
|
* so some association functions is available
|
74
74
|
* and the :dependent action is triggered when destroying some related model
|
75
|
+
* some validation like presense, length restriction and format are supported
|
75
76
|
* databases specific functions between mysql, postgres are not covered yet
|
76
77
|
* model callbacks are not available
|
77
78
|
* sql.NullType not supported yet, so you'd better in the migrations set those columns "not null" with a default value that's consistent with Golang's zero value specification, such as "" default for string and text typed column, and 0 default for int, etc.
|
@@ -86,7 +87,7 @@ When I had the idea to convert Rails app or build Golang app with Rails tools, I
|
|
86
87
|
|
87
88
|
There're two branches at present: `master` and `dev`.
|
88
89
|
|
89
|
-
The
|
90
|
+
The `dev` branch has a whole Rails environment for development: models, seeds for testing, and under `go_app` directory there's a file named `modles_test.go` used to test generated Golang codes.
|
90
91
|
|
91
92
|
- Fork the project switch to branch `dev`.
|
92
93
|
- Make your feature addition or bug fix.
|
@@ -11,7 +11,7 @@ module GoOnRails
|
|
11
11
|
def get_schema_info
|
12
12
|
info = {struct_body: "", has_assoc_dependent: false, assoc_info: {has_many: {}, has_one: {}, belongs_to: {}}}
|
13
13
|
self.klass.reflect_on_all_associations.each do |assoc|
|
14
|
-
tags = ["json:\"#{assoc.name.to_s},omitempty\" db:\"#{assoc.name.to_s}\""]
|
14
|
+
tags = ["json:\"#{assoc.name.to_s},omitempty\" db:\"#{assoc.name.to_s}\" valid:\"-\""]
|
15
15
|
case assoc.macro
|
16
16
|
when :has_many
|
17
17
|
col_name = assoc.name.to_s.camelize
|
@@ -33,12 +33,13 @@ module GoOnRails
|
|
33
33
|
|
34
34
|
self.max_col_size = get_max_col_size
|
35
35
|
self.max_type_size = get_max_type_size
|
36
|
+
validation = GoOnRails::Validator.new(self.klass)
|
36
37
|
|
37
38
|
self.klass.columns.each_with_index do |col, index|
|
38
39
|
tags = []
|
39
40
|
|
40
41
|
# add struct tag
|
41
|
-
tags << struct_tag(col)
|
42
|
+
tags << struct_tag(col, validation)
|
42
43
|
|
43
44
|
col_type = col.type.to_s
|
44
45
|
struct_info[:has_datetime_type] = true if %w(datetime time).include? col_type
|
@@ -88,11 +89,12 @@ module GoOnRails
|
|
88
89
|
builder.get_schema_info
|
89
90
|
end
|
90
91
|
|
91
|
-
def struct_tag(col)
|
92
|
-
|
92
|
+
def struct_tag(col, validation)
|
93
|
+
valid_tags = validation.build_validator_tag(col)
|
94
|
+
"json:\"#{col.name},omitempty\" db:\"#{col.name}\" #{valid_tags}"
|
93
95
|
end
|
94
|
-
|
95
96
|
end
|
96
97
|
end
|
97
98
|
|
98
99
|
require_relative 'association'
|
100
|
+
require_relative 'validator'
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module GoOnRails
|
2
|
+
class Validator
|
3
|
+
TAG_DELIM = ","
|
4
|
+
|
5
|
+
def initialize(klass)
|
6
|
+
@klass = klass
|
7
|
+
end
|
8
|
+
attr_reader :klass
|
9
|
+
|
10
|
+
def build_validator_tag(col)
|
11
|
+
# ignore checking for some automaticly created fields
|
12
|
+
return "valid:\"-\"" if %w(id created_at updated_at).include?(col.name)
|
13
|
+
tags = []
|
14
|
+
|
15
|
+
validators = self.klass.validators_on col.name
|
16
|
+
|
17
|
+
validators.each do |validator|
|
18
|
+
tags.concat get_validation(validator, col)
|
19
|
+
end
|
20
|
+
|
21
|
+
tags << 'optional' if col.null && !tags.empty? && tags.exclude?('required')
|
22
|
+
|
23
|
+
if tags.present?
|
24
|
+
return "valid:\"#{tags.uniq.join(TAG_DELIM)}\""
|
25
|
+
else
|
26
|
+
return "valid:\"-\""
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_validation(validator, col)
|
31
|
+
rules = []
|
32
|
+
case validator.class.to_s
|
33
|
+
when "ActiveRecord::Validations::PresenceValidator"
|
34
|
+
rules << "required"
|
35
|
+
when "ActiveModel::Validations::FormatValidator"
|
36
|
+
if validator.options && validator.options[:with] && ['string', 'text'].include?(col.type.to_s)
|
37
|
+
re = $1 || ".*" if validator.options[:with].inspect =~ /\/(.*)\//
|
38
|
+
rules << "matches(#{re})"
|
39
|
+
end
|
40
|
+
when "ActiveModel::Validations::NumericalityValidator"
|
41
|
+
if validator.options && validator.options[:only_integer]
|
42
|
+
rules << "IsInt"
|
43
|
+
else
|
44
|
+
rules << "IsNumeric"
|
45
|
+
end
|
46
|
+
when "ActiveRecord::Validations::LengthValidator"
|
47
|
+
if validator.options && ['string', 'text'].include?(col.type.to_s)
|
48
|
+
if validator.options[:is]
|
49
|
+
min = max = validator.options[:is]
|
50
|
+
else
|
51
|
+
min = validator.options[:minimum] ? validator.options[:minimum] : 0
|
52
|
+
max = validator.options[:maximum] ? validator.options[:maximum] : 4_294_967_295
|
53
|
+
end
|
54
|
+
rules << sprintf("length(%d|%d)", min, max)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
rules
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -16,6 +16,8 @@ import (
|
|
16
16
|
<%- if @struct_info[:has_datetime_type] -%>
|
17
17
|
"time"
|
18
18
|
<%- end -%>
|
19
|
+
|
20
|
+
"github.com/asaskevich/govalidator"
|
19
21
|
)
|
20
22
|
|
21
23
|
type <%= @model_name %> struct {
|
@@ -318,6 +320,15 @@ func Create<%= @model_name %>(am map[string]interface{}) (int64, error) {
|
|
318
320
|
}
|
319
321
|
|
320
322
|
func (var_<%= model_name_underscore %> *<%= @model_name %>) Create() error {
|
323
|
+
ok, err := govalidator.ValidateStruct(var_<%= model_name_underscore %>)
|
324
|
+
if !ok {
|
325
|
+
errMsg := "Validate <%= @model_name %> struct error: Unknown error"
|
326
|
+
if err != nil {
|
327
|
+
errMsg = "Validate <%= @model_name %> struct error: " + err.Error()
|
328
|
+
}
|
329
|
+
log.Println(errMsg)
|
330
|
+
return errors.New(errMsg)
|
331
|
+
}
|
321
332
|
<%- unless @struct_info[:timestamp_cols].empty? -%>
|
322
333
|
t := time.Now()
|
323
334
|
<%- @struct_info[:timestamp_cols].each do |c| -%>
|
@@ -325,7 +336,7 @@ func (var_<%= model_name_underscore %> *<%= @model_name %>) Create() error {
|
|
325
336
|
<%- end -%>
|
326
337
|
<%- end -%>
|
327
338
|
sql := `INSERT INTO <%= table_name %> (<%= col_names.join(",") %>) VALUES (:<%= col_names.join(",:") %>)`
|
328
|
-
_, err
|
339
|
+
_, err = db.NamedExec(sql, var_<%= model_name_underscore %>)
|
329
340
|
return err
|
330
341
|
}
|
331
342
|
|
@@ -561,6 +572,15 @@ func destroy<%= @model_name %>Associations(ids ...int64) {
|
|
561
572
|
<%- end -%>
|
562
573
|
|
563
574
|
func (var_<%= model_name_underscore %> *<%= @model_name %>) Save() error {
|
575
|
+
ok, err := govalidator.ValidateStruct(var_<%= model_name_underscore %>)
|
576
|
+
if !ok {
|
577
|
+
errMsg := "Validate <%= @model_name %> struct error: Unknown error"
|
578
|
+
if err != nil {
|
579
|
+
errMsg = "Validate <%= @model_name %> struct error: " + err.Error()
|
580
|
+
}
|
581
|
+
log.Println(errMsg)
|
582
|
+
return errors.New(errMsg)
|
583
|
+
}
|
564
584
|
if var_<%= model_name_underscore %>.Id == 0 {
|
565
585
|
return errors.New("Invalid Id field: it can't be a zero value")
|
566
586
|
}
|
@@ -570,7 +590,7 @@ func (var_<%= model_name_underscore %> *<%= @model_name %>) Save() error {
|
|
570
590
|
sqlFmt := `UPDATE <%= table_name %> SET %s WHERE id = %v`
|
571
591
|
<%- save_col_names = col_names - ["created_at"] -%>
|
572
592
|
sqlStr := fmt.Sprintf(sqlFmt, "<%= save_col_names.zip(save_col_names).map{|c| c.join(" = :")}.join(", ") %>", var_<%= model_name_underscore %>.Id)
|
573
|
-
_, err
|
593
|
+
_, err = db.NamedExec(sqlStr, var_<%= model_name_underscore %>)
|
574
594
|
return err
|
575
595
|
}
|
576
596
|
|
data/lib/tasks/gor.rake
CHANGED
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.
|
4
|
+
version: 0.0.8
|
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-
|
11
|
+
date: 2017-05-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
|
@@ -23,6 +23,7 @@ files:
|
|
23
23
|
- lib/generators/gor/USAGE
|
24
24
|
- lib/generators/gor/go-on-rails/association.rb
|
25
25
|
- lib/generators/gor/go-on-rails/converter.rb
|
26
|
+
- lib/generators/gor/go-on-rails/validator.rb
|
26
27
|
- lib/generators/gor/gor_generator.rb
|
27
28
|
- lib/generators/gor/templates/db.go.erb
|
28
29
|
- lib/generators/gor/templates/favicon.ico
|