ar2gostruct 0.1.1 → 0.2.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 +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +5 -5
- data/CHANGELOG.md +7 -0
- data/Gemfile +10 -4
- data/README.md +7 -3
- data/Rakefile +7 -0
- data/ar2gostruct.gemspec +1 -0
- data/bin/ar2gostruct +4 -0
- data/lib/ar2gostruct/builder/association.rb +32 -0
- data/lib/ar2gostruct/builder/orm/base.rb +15 -0
- data/lib/ar2gostruct/builder/orm/gorm.rb +35 -0
- data/lib/ar2gostruct/builder/orm/qbs.rb +38 -0
- data/lib/ar2gostruct/const.rb +17 -0
- data/lib/ar2gostruct/converter.rb +104 -0
- data/lib/ar2gostruct/gostruct.rb +40 -0
- data/lib/ar2gostruct/version.rb +1 -1
- data/lib/ar2gostruct.rb +12 -102
- data/spec/ar2gostruct/ar2gostruct_spec.rb +5 -0
- data/spec/ar2gostruct/builder/association_spec.rb +43 -0
- data/spec/ar2gostruct/builder/orm/gorm_spec.rb +38 -0
- data/spec/ar2gostruct/builder/orm/qbs_spec.rb +38 -0
- data/spec/db/database.yml +5 -0
- data/spec/db/migrate/0001_create_tables.rb +33 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/support/models.rb +24 -0
- metadata +32 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa224af8ae2ec6ea6829496a2e3bdda2539f9a7f
|
4
|
+
data.tar.gz: 9fdc2a6f65cd429b3fef64b817f82dc816dbff90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b95874bb14f79adb8169afc43b97205357e6dd071b960d44e7dd86d73aeb3c3425ceb023bc56d2ad30b082ed931e2bf38d6d74379d5f19b658f5f17a69571e6b
|
7
|
+
data.tar.gz: a6db670082617c894c8ae56e0fd1bf6de5cb5df7d412a8ebf9db58ad6468fbac89559896993cdb644a40b5b70fd1a8af07fb8a87071c66f1611eddc45c87c2aa
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
@@ -1,19 +1,25 @@
|
|
1
|
-
source
|
1
|
+
source "https://rubygems.org"
|
2
2
|
|
3
3
|
group :development do
|
4
4
|
gem "yard"
|
5
5
|
end
|
6
6
|
|
7
7
|
group :development, :test do
|
8
|
-
gem
|
8
|
+
gem "simplecov"
|
9
|
+
platform :ruby do
|
10
|
+
gem "sqlite3"
|
11
|
+
end
|
12
|
+
platform :jruby do
|
13
|
+
gem 'activerecord-jdbcsqlite3-adapter'
|
14
|
+
end
|
9
15
|
|
10
16
|
if RUBY_PLATFORM =~ /darwin/
|
11
17
|
gem "ruby_gntp"
|
12
18
|
gem "rb-fsevent"
|
13
19
|
end
|
14
20
|
if RUBY_PLATFORM =~ /linux/
|
15
|
-
gem
|
16
|
-
gem
|
21
|
+
gem "libnotify"
|
22
|
+
gem "rb-inotify"
|
17
23
|
end
|
18
24
|
end
|
19
25
|
gemspec
|
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# Ar2gostruct
|
2
2
|
[](https://rubygems.org/gems/ar2gostruct)
|
3
|
+
[](https://travis-ci.org/t-k/ar2gostruct)
|
3
4
|
|
4
5
|
Automatically generate Golang Struct from your activerecord models.
|
5
6
|
|
@@ -23,9 +24,10 @@ Usage
|
|
23
24
|
|
24
25
|
On your rails directory.
|
25
26
|
```bash
|
26
|
-
bundle exec rake ar2gostruct
|
27
|
+
bundle exec rake ar2gostruct association=true
|
28
|
+
# association option enables Active Record Associations (Needs ar2gostruct v0.2.0 or greater).
|
27
29
|
# or
|
28
|
-
ar2gostruct
|
30
|
+
bundle exec ar2gostruct --a
|
29
31
|
```
|
30
32
|
this will returns
|
31
33
|
```bash
|
@@ -49,10 +51,12 @@ type User struct {
|
|
49
51
|
UnconfirmedEmail string `json:"unconfirmed_email"`
|
50
52
|
CreatedAt time.Time `json:"created_at"`
|
51
53
|
UpdatedAt time.Time `json:"updated_at"`
|
54
|
+
Profile Profile `json:"profile"`
|
55
|
+
Projects []Project `json:"projects"`
|
52
56
|
}
|
53
57
|
```
|
54
58
|
|
55
|
-
If you're using [qbs](https://github.com/coocood/qbs#), Additional options are available.
|
59
|
+
If you're using [qbs](https://github.com/coocood/qbs#) or [gorm](https://github.com/jinzhu/gorm), Additional options are available.
|
56
60
|
|
57
61
|
```bash
|
58
62
|
bundle exec rake ar2gostruct orm=qbs
|
data/Rakefile
CHANGED
data/ar2gostruct.gemspec
CHANGED
data/bin/ar2gostruct
CHANGED
@@ -12,6 +12,10 @@ OptionParser.new do |opt|
|
|
12
12
|
puts "ar2gostruct v#{Ar2gostruct.version}"; exit
|
13
13
|
end
|
14
14
|
|
15
|
+
opt.on("--association", "Enable association") do
|
16
|
+
ENV["association"] = "true"
|
17
|
+
end
|
18
|
+
|
15
19
|
opt.on("--orm orm", "Specify ORM. Currently supports only qbs") do |orm|
|
16
20
|
ENV["orm"] = orm.to_s
|
17
21
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Ar2gostruct
|
2
|
+
module Builder
|
3
|
+
class Association
|
4
|
+
def initialize(klass, max_col_size, max_type_size)
|
5
|
+
@klass = klass
|
6
|
+
@max_col_size = max_col_size
|
7
|
+
@max_type_size = max_type_size
|
8
|
+
end
|
9
|
+
attr_reader :klass, :max_col_size, :max_type_size
|
10
|
+
|
11
|
+
def get_schema_info
|
12
|
+
info = ""
|
13
|
+
self.klass.reflect_on_all_associations.each do |assoc|
|
14
|
+
tags = ["json:\"#{assoc.name.to_s}\""]
|
15
|
+
case assoc.macro
|
16
|
+
when :has_many
|
17
|
+
col_name = assoc.name.to_s.camelize
|
18
|
+
type_name = "[]#{assoc.name.to_s.singularize.camelize}"
|
19
|
+
when :has_one, :belongs_to
|
20
|
+
col_name = assoc.name.to_s.camelize
|
21
|
+
type_name = col_name
|
22
|
+
end
|
23
|
+
if col_name && type_name
|
24
|
+
info << sprintf("\t%-#{self.max_col_size}.#{self.max_col_size+2}s%-#{self.max_type_size}.#{self.max_type_size}s`%s`\n", col_name, type_name, tags.join(" "))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
info
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# GORM https://github.com/jinzhu/gorm
|
2
|
+
require "ar2gostruct/builder/orm/base"
|
3
|
+
module Ar2gostruct
|
4
|
+
module Builder
|
5
|
+
module ORM
|
6
|
+
class GORM < Base
|
7
|
+
|
8
|
+
TAG_SEPARATOR = ";"
|
9
|
+
|
10
|
+
def get_option(col)
|
11
|
+
orm_option = []
|
12
|
+
# not null Constraint
|
13
|
+
unless col.null
|
14
|
+
orm_option << "not null"
|
15
|
+
end
|
16
|
+
# set size
|
17
|
+
if col.type == :string
|
18
|
+
# SQL type
|
19
|
+
if col.sql_type
|
20
|
+
orm_option << "type:#{col.sql_type}"
|
21
|
+
end
|
22
|
+
orm_option << "size:#{col.limit}"
|
23
|
+
end
|
24
|
+
|
25
|
+
if orm_option.present?
|
26
|
+
return "sql:\"#{orm_option.join(TAG_SEPARATOR)}\""
|
27
|
+
else
|
28
|
+
return nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# QBS https://github.com/coocood/qbs
|
2
|
+
require "ar2gostruct/builder/orm/base"
|
3
|
+
module Ar2gostruct
|
4
|
+
module Builder
|
5
|
+
module ORM
|
6
|
+
class QBS < Base
|
7
|
+
|
8
|
+
def get_option(col)
|
9
|
+
orm_option = []
|
10
|
+
# primary key
|
11
|
+
if col.name == @klass.primary_key
|
12
|
+
orm_option << "pk"
|
13
|
+
end
|
14
|
+
# not null Constraint
|
15
|
+
unless col.null
|
16
|
+
orm_option << "notnull"
|
17
|
+
end
|
18
|
+
# default value
|
19
|
+
if col.default
|
20
|
+
orm_option << "default:'#{col.default}'"
|
21
|
+
end
|
22
|
+
# set timestamp
|
23
|
+
if col.name == "created_at"
|
24
|
+
orm_option << "created"
|
25
|
+
elsif col.name == "updated_at"
|
26
|
+
orm_option << "updated"
|
27
|
+
end
|
28
|
+
if orm_option.present?
|
29
|
+
return "qbs:\"#{orm_option.join(TAG_SEPARATOR)}\""
|
30
|
+
else
|
31
|
+
return nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Ar2gostruct
|
2
|
+
module CONST
|
3
|
+
TYPE_MAP = {
|
4
|
+
"string" => "string",
|
5
|
+
"text" => "string",
|
6
|
+
"boolean" => "bool",
|
7
|
+
"integer(1)" => "int8",
|
8
|
+
"integer(2)" => "int16",
|
9
|
+
"integer(3)" => "int32",
|
10
|
+
"integer(4)" => "int32",
|
11
|
+
"integer(8)" => "int64",
|
12
|
+
"float" => "float64",
|
13
|
+
"datetime" => "time.Time",
|
14
|
+
"date" => "time.Time"
|
15
|
+
}
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
module Ar2gostruct
|
2
|
+
class Converter
|
3
|
+
def initialize(klass, option = {})
|
4
|
+
@klass = klass
|
5
|
+
@max_col_size = 0
|
6
|
+
@max_type_size = 0
|
7
|
+
@plural = option[:plural]
|
8
|
+
@orm = option[:orm]
|
9
|
+
@association = option[:association]
|
10
|
+
end
|
11
|
+
attr_accessor :klass, :max_col_size, :max_type_size, :plural, :orm, :association
|
12
|
+
|
13
|
+
def convert!
|
14
|
+
get_schema_info
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def get_schema_info
|
20
|
+
info = "// Table name: #{self.klass.table_name}\n"
|
21
|
+
struct_name = get_struct_name
|
22
|
+
info << "type #{struct_name} struct {\n"
|
23
|
+
|
24
|
+
self.max_col_size = get_max_col_size
|
25
|
+
self.max_type_size = get_max_type_size
|
26
|
+
|
27
|
+
self.klass.columns.each do |col|
|
28
|
+
tags = []
|
29
|
+
|
30
|
+
# add json tag
|
31
|
+
tags << json_tag(col)
|
32
|
+
|
33
|
+
if self.orm
|
34
|
+
orm_options = get_orm_options(col)
|
35
|
+
tags << orm_options if orm_options && orm_options.length > 0
|
36
|
+
end
|
37
|
+
|
38
|
+
col_type = col.type.to_s
|
39
|
+
case col_type
|
40
|
+
when "integer"
|
41
|
+
type = CONST::TYPE_MAP["integer(#{col.limit})"] || "int32"
|
42
|
+
type = "u#{type}" if col.sql_type.match("unsigned").present?
|
43
|
+
else
|
44
|
+
type = CONST::TYPE_MAP[col_type] || "string"
|
45
|
+
end
|
46
|
+
|
47
|
+
info << sprintf("\t%-#{self.max_col_size}.#{self.max_col_size}s%-#{self.max_type_size}.#{self.max_type_size}s`%s`\n", col.name.camelize, type, tags.join(" "))
|
48
|
+
|
49
|
+
end
|
50
|
+
info << get_associations if self.association
|
51
|
+
|
52
|
+
info << "}\n\n"
|
53
|
+
return info
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_max_col_size
|
57
|
+
col_name_max_size = self.klass.column_names.collect{|name| name.size}.max || 0
|
58
|
+
assoc_max_size = if self.association
|
59
|
+
self.klass.reflect_on_all_associations.collect{|assoc| assoc.name.to_s.size}.max || 0
|
60
|
+
else
|
61
|
+
0
|
62
|
+
end
|
63
|
+
type_max_size = Ar2gostruct::CONST::TYPE_MAP.collect{|key, value| key.size}.max || 0
|
64
|
+
[col_name_max_size + 1, assoc_max_size, type_max_size].max
|
65
|
+
end
|
66
|
+
|
67
|
+
def get_max_type_size
|
68
|
+
assoc_max_size = if self.association
|
69
|
+
self.klass.reflect_on_all_associations.collect{|assoc| assoc.name.to_s.size + 2}.max || 0
|
70
|
+
else
|
71
|
+
0
|
72
|
+
end
|
73
|
+
type_max_size = Ar2gostruct::CONST::TYPE_MAP.collect{|key, value| key.size}.max || 0
|
74
|
+
[assoc_max_size, type_max_size].max
|
75
|
+
end
|
76
|
+
|
77
|
+
def get_orm_options(col)
|
78
|
+
tags ||= []
|
79
|
+
builder = "Ar2gostruct::Builder::ORM::#{self.orm.upcase}".constantize.new(self.klass)
|
80
|
+
option = builder.get_option col
|
81
|
+
tags << option if option
|
82
|
+
rescue => e
|
83
|
+
[]
|
84
|
+
end
|
85
|
+
|
86
|
+
def get_struct_name
|
87
|
+
if self.plural
|
88
|
+
self.klass.table_name.camelize
|
89
|
+
else
|
90
|
+
self.klass.to_s.tr_s('::', '')
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def get_associations
|
95
|
+
builder = Ar2gostruct::Builder::Association.new(self.klass, self.max_col_size, self.max_type_size)
|
96
|
+
builder.get_schema_info
|
97
|
+
end
|
98
|
+
|
99
|
+
def json_tag(col)
|
100
|
+
"json:\"#{col.name}\""
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Ar2gostruct
|
2
|
+
class Gostruct
|
3
|
+
def initialize(model_dir)
|
4
|
+
@model_dir = model_dir
|
5
|
+
get_models
|
6
|
+
end
|
7
|
+
attr_accessor :model_dir, :models
|
8
|
+
|
9
|
+
def convert!
|
10
|
+
self.models.each do |m|
|
11
|
+
class_name = m.sub(/\.rb$/,'').camelize
|
12
|
+
begin
|
13
|
+
klass = class_name.split('::').inject(Object){ |klass,part| klass.const_get(part) }
|
14
|
+
if klass < ActiveRecord::Base && !klass.abstract_class?
|
15
|
+
convert_to_gostruct!(klass)
|
16
|
+
end
|
17
|
+
rescue Exception => e
|
18
|
+
puts "// Unable to convert #{class_name}: #{e.message}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def get_models
|
26
|
+
Dir.chdir(self.model_dir) do
|
27
|
+
self.models = Dir["**/*.rb"]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def convert_to_gostruct!(klass)
|
32
|
+
converter = Converter.new klass, :plural => ENV["plural"], :orm => ENV["orm"], :association => ENV["association"]
|
33
|
+
info = converter.convert!
|
34
|
+
model_file_name = File.join(self.model_dir, klass.name.underscore + ".rb")
|
35
|
+
puts "// #{model_file_name}"
|
36
|
+
puts info
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
data/lib/ar2gostruct/version.rb
CHANGED
data/lib/ar2gostruct.rb
CHANGED
@@ -17,21 +17,7 @@ require "active_record"
|
|
17
17
|
require "ar2gostruct/railtie" if defined?(Rails)
|
18
18
|
|
19
19
|
module Ar2gostruct
|
20
|
-
MODEL_DIR
|
21
|
-
|
22
|
-
TYPE_MAP = {
|
23
|
-
"string" => "string",
|
24
|
-
"text" => "string",
|
25
|
-
"boolean" => "bool",
|
26
|
-
"integer(1)" => "int8",
|
27
|
-
"integer(2)" => "int16",
|
28
|
-
"integer(3)" => "int32",
|
29
|
-
"integer(4)" => "int32",
|
30
|
-
"integer(8)" => "int64",
|
31
|
-
"float" => "float64",
|
32
|
-
"datetime" => "time.Time",
|
33
|
-
"date" => "time.Time"
|
34
|
-
}
|
20
|
+
MODEL_DIR = ENV["model_dir"] || "app/models"
|
35
21
|
|
36
22
|
def self.load
|
37
23
|
path = ENV["require_path"] || "#{Dir.pwd}/config/environment"
|
@@ -43,93 +29,17 @@ module Ar2gostruct
|
|
43
29
|
end
|
44
30
|
end
|
45
31
|
|
46
|
-
def self.get_schema_info(klass)
|
47
|
-
info = "// Table name: #{klass.table_name}\n"
|
48
|
-
if ENV["plural"]
|
49
|
-
struct_name = klass.table_name.camelize
|
50
|
-
else
|
51
|
-
struct_name = klass.to_s.tr_s('::', '')
|
52
|
-
end
|
53
|
-
info << "type #{struct_name} struct {\n"
|
54
|
-
|
55
|
-
max_size = klass.column_names.collect{|name| name.size}.max + 1
|
56
|
-
klass.columns.each do |col|
|
57
|
-
tags = []
|
58
|
-
|
59
|
-
# add json tag
|
60
|
-
tags << "json:\"#{col.name}\""
|
61
|
-
|
62
|
-
case ENV["orm"]
|
63
|
-
when "qbs"
|
64
|
-
orm_option = []
|
65
|
-
# primary key
|
66
|
-
if col.name == klass.primary_key
|
67
|
-
orm_option << "pk"
|
68
|
-
end
|
69
|
-
# not null Constraint
|
70
|
-
unless col.null
|
71
|
-
orm_option << "notnull"
|
72
|
-
end
|
73
|
-
# default value
|
74
|
-
if col.default
|
75
|
-
orm_option << "default:'#{col.default}'"
|
76
|
-
end
|
77
|
-
# set timestamp
|
78
|
-
if col.name == "created_at"
|
79
|
-
orm_option << "created"
|
80
|
-
elsif col.name == "updated_at"
|
81
|
-
orm_option << "updated"
|
82
|
-
end
|
83
|
-
if orm_option.present?
|
84
|
-
tags << "qbs:\"#{orm_option.join(",")}\""
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
col_type = col.type.to_s
|
89
|
-
case col_type
|
90
|
-
when "integer"
|
91
|
-
type = TYPE_MAP["integer(#{col.limit})"] || "int32"
|
92
|
-
type = "u#{type}" if col.sql_type.match("unsigned").present?
|
93
|
-
else
|
94
|
-
type = TYPE_MAP[col_type] || "string"
|
95
|
-
end
|
96
|
-
|
97
|
-
info << sprintf("\t%-#{max_size}.#{max_size}s%-15.15s`%s`\n", col.name.camelize, type, tags.join(" "))
|
98
|
-
|
99
|
-
end
|
100
|
-
|
101
|
-
info << "}\n\n"
|
102
|
-
end
|
103
|
-
|
104
|
-
def self.convert_to_gostruct(klass)
|
105
|
-
info = get_schema_info(klass)
|
106
|
-
|
107
|
-
model_file_name = File.join(MODEL_DIR, klass.name.underscore + ".rb")
|
108
|
-
|
109
|
-
puts "// #{model_file_name}"
|
110
|
-
puts info
|
111
|
-
end
|
112
|
-
|
113
|
-
def self.get_model_names
|
114
|
-
models = []
|
115
|
-
Dir.chdir(MODEL_DIR) do
|
116
|
-
models = Dir["**/*.rb"]
|
117
|
-
end
|
118
|
-
models
|
119
|
-
end
|
120
|
-
|
121
32
|
def self.convert!
|
122
|
-
|
123
|
-
|
124
|
-
begin
|
125
|
-
klass = class_name.split('::').inject(Object){ |klass,part| klass.const_get(part) }
|
126
|
-
if klass < ActiveRecord::Base && !klass.abstract_class?
|
127
|
-
self.convert_to_gostruct(klass)
|
128
|
-
end
|
129
|
-
rescue Exception => e
|
130
|
-
puts "// Unable to convert #{class_name}: #{e.message}"
|
131
|
-
end
|
132
|
-
|
133
|
-
end
|
33
|
+
gostruct = Gostruct.new(MODEL_DIR)
|
34
|
+
gostruct.convert!
|
134
35
|
end
|
36
|
+
|
135
37
|
end
|
38
|
+
|
39
|
+
require "ar2gostruct/const"
|
40
|
+
require "ar2gostruct/converter"
|
41
|
+
require "ar2gostruct/gostruct"
|
42
|
+
require "ar2gostruct/builder/association"
|
43
|
+
require "ar2gostruct/builder/orm/gorm"
|
44
|
+
require "ar2gostruct/builder/orm/qbs"
|
45
|
+
require "ar2gostruct/builder/association"
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Ar2gostruct::Builder::Association" do
|
4
|
+
|
5
|
+
def get_max_col_size(klass)
|
6
|
+
col_name_max_size = klass.column_names.collect{|name| name.size}.max || 0
|
7
|
+
assoc_max_size = klass.reflect_on_all_associations.collect{|assoc| assoc.name.to_s.size}.max || 0
|
8
|
+
type_max_size = Ar2gostruct::CONST::TYPE_MAP.collect{|key, value| key.size}.max || 0
|
9
|
+
[col_name_max_size, assoc_max_size, type_max_size].max
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_max_type_size(klass)
|
13
|
+
assoc_max_size = klass.reflect_on_all_associations.collect{|assoc| assoc.name.to_s.size + 2}.max || 0
|
14
|
+
type_max_size = Ar2gostruct::CONST::TYPE_MAP.collect{|key, value| key.size}.max || 0
|
15
|
+
[assoc_max_size, type_max_size].max
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#get_schema_info" do
|
19
|
+
it "should output associations" do
|
20
|
+
klass = User
|
21
|
+
builder = Ar2gostruct::Builder::Association.new(klass, get_max_col_size(klass), get_max_type_size(klass))
|
22
|
+
result = builder.get_schema_info
|
23
|
+
expect(result).to match(/Profile/)
|
24
|
+
expect(result).to match(/json:\"profile\"`/)
|
25
|
+
expect(result).to match(/Projects/)
|
26
|
+
expect(result).to match(/\[\]Project/)
|
27
|
+
expect(result).to match(/json:\"projects\"`/)
|
28
|
+
|
29
|
+
klass = Profile
|
30
|
+
builder = Ar2gostruct::Builder::Association.new(klass, get_max_col_size(klass), get_max_type_size(klass))
|
31
|
+
result = builder.get_schema_info
|
32
|
+
expect(result).to match(/User/)
|
33
|
+
expect(result).to match(/json:\"user\"`/)
|
34
|
+
|
35
|
+
klass = Project
|
36
|
+
builder = Ar2gostruct::Builder::Association.new(klass, get_max_col_size(klass), get_max_type_size(klass))
|
37
|
+
result = builder.get_schema_info
|
38
|
+
expect(result).to match(/User/)
|
39
|
+
expect(result).to match(/json:\"user\"`/)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Ar2gostruct::Builder::ORM::GORM" do
|
4
|
+
|
5
|
+
describe "#get_option" do
|
6
|
+
it "should output GORM format tags" do
|
7
|
+
orm_converter = Ar2gostruct::Builder::ORM::GORM.new User
|
8
|
+
# id
|
9
|
+
result = orm_converter.get_option User.columns[0]
|
10
|
+
expect(result).to eq("sql:\"not null\"")
|
11
|
+
# email
|
12
|
+
result = orm_converter.get_option User.columns[1]
|
13
|
+
expect(result).to eq("sql:\"not null;type:varchar(255);size:255\"")
|
14
|
+
# sign_in_count
|
15
|
+
result = orm_converter.get_option User.columns[2]
|
16
|
+
expect(result).to eq("sql:\"not null\"")
|
17
|
+
# current_sign_in_at
|
18
|
+
result = orm_converter.get_option User.columns[3]
|
19
|
+
expect(result).to eq(nil)
|
20
|
+
# last_sign_in_at
|
21
|
+
result = orm_converter.get_option User.columns[4]
|
22
|
+
expect(result).to eq(nil)
|
23
|
+
# current_sign_in_ip
|
24
|
+
result = orm_converter.get_option User.columns[5]
|
25
|
+
expect(result).to eq("sql:\"type:varchar(40);size:40\"")
|
26
|
+
# last_sign_in_ip
|
27
|
+
result = orm_converter.get_option User.columns[6]
|
28
|
+
expect(result).to eq("sql:\"type:varchar(40);size:40\"")
|
29
|
+
# created_at
|
30
|
+
result = orm_converter.get_option User.columns[7]
|
31
|
+
expect(result).to eq(nil)
|
32
|
+
# updated_at
|
33
|
+
result = orm_converter.get_option User.columns[8]
|
34
|
+
expect(result).to eq(nil)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Ar2gostruct::Builder::ORM::QBS" do
|
4
|
+
|
5
|
+
describe "#get_option" do
|
6
|
+
it "should output QBS format tags" do
|
7
|
+
orm_converter = Ar2gostruct::Builder::ORM::QBS.new User
|
8
|
+
# id
|
9
|
+
result = orm_converter.get_option User.columns[0]
|
10
|
+
expect(result).to eq("qbs:\"pk,notnull\"")
|
11
|
+
# email
|
12
|
+
result = orm_converter.get_option User.columns[1]
|
13
|
+
expect(result).to eq("qbs:\"notnull,default:''\"")
|
14
|
+
# sign_in_count
|
15
|
+
result = orm_converter.get_option User.columns[2]
|
16
|
+
expect(result).to eq("qbs:\"notnull,default:'0'\"")
|
17
|
+
# current_sign_in_at
|
18
|
+
result = orm_converter.get_option User.columns[3]
|
19
|
+
expect(result).to eq(nil)
|
20
|
+
# last_sign_in_at
|
21
|
+
result = orm_converter.get_option User.columns[4]
|
22
|
+
expect(result).to eq(nil)
|
23
|
+
# current_sign_in_ip
|
24
|
+
result = orm_converter.get_option User.columns[5]
|
25
|
+
expect(result).to eq(nil)
|
26
|
+
# last_sign_in_ip
|
27
|
+
result = orm_converter.get_option User.columns[6]
|
28
|
+
expect(result).to eq(nil)
|
29
|
+
# created_at
|
30
|
+
result = orm_converter.get_option User.columns[7]
|
31
|
+
expect(result).to eq("qbs:\"created\"")
|
32
|
+
# updated_at
|
33
|
+
result = orm_converter.get_option User.columns[8]
|
34
|
+
expect(result).to eq("qbs:\"updated\"")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class CreateTables < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :users do |t|
|
4
|
+
t.string :email, :null => false, :default => ""
|
5
|
+
t.integer :sign_in_count, :null => false, :default => 0
|
6
|
+
t.datetime :current_sign_in_at
|
7
|
+
t.datetime :last_sign_in_at
|
8
|
+
t.string :current_sign_in_ip, :limit => 40
|
9
|
+
t.string :last_sign_in_ip, :limit => 40
|
10
|
+
t.timestamps
|
11
|
+
end
|
12
|
+
add_index :users, :email, :unique => true
|
13
|
+
|
14
|
+
create_table :profiles do |t|
|
15
|
+
t.integer :user_id, :null => false
|
16
|
+
t.string :first_name, :null => false, :default => "", :limit => 80
|
17
|
+
t.string :last_name, :null => false, :default => "", :limit => 80
|
18
|
+
t.text :bio
|
19
|
+
t.date :dob
|
20
|
+
t.timestamps
|
21
|
+
end
|
22
|
+
add_index :profiles, :user_id, :unique => true
|
23
|
+
|
24
|
+
create_table :projects do |t|
|
25
|
+
t.integer :user_id, :null => false
|
26
|
+
t.string :name, :null => false, :default => "", :limit => 150
|
27
|
+
t.integer :status, :null => false, :limit => 1, :default => 1
|
28
|
+
t.boolean :is_private, :null => false, :default => true
|
29
|
+
t.timestamps
|
30
|
+
end
|
31
|
+
add_index :projects, :user_id
|
32
|
+
end
|
33
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
begin
|
2
|
+
require "bundler/setup"
|
3
|
+
rescue LoadError
|
4
|
+
puts "Although not required, bundler is recommended for running the tests."
|
5
|
+
end
|
6
|
+
# load the library
|
7
|
+
require "simplecov"
|
8
|
+
SimpleCov.start :test_frameworks do
|
9
|
+
add_filter "/vendor/bundle/"
|
10
|
+
end
|
11
|
+
|
12
|
+
require "ar2gostruct"
|
13
|
+
require "rspec"
|
14
|
+
require "support/models"
|
15
|
+
|
16
|
+
RSpec.configure do |config|
|
17
|
+
config.mock_with :rspec
|
18
|
+
config.filter_run :focus => true
|
19
|
+
config.run_all_when_everything_filtered = true
|
20
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "active_record"
|
2
|
+
|
3
|
+
class User < ActiveRecord::Base
|
4
|
+
has_one :profile
|
5
|
+
has_many :projects
|
6
|
+
end
|
7
|
+
|
8
|
+
class Profile < ActiveRecord::Base
|
9
|
+
belongs_to :user
|
10
|
+
end
|
11
|
+
|
12
|
+
class Project < ActiveRecord::Base
|
13
|
+
belongs_to :user
|
14
|
+
end
|
15
|
+
|
16
|
+
Dir::mkdir("db") unless File.exists?("db")
|
17
|
+
|
18
|
+
ActiveRecord::Base.establish_connection \
|
19
|
+
:adapter => "sqlite3",
|
20
|
+
:database => "db/test.db",
|
21
|
+
:pool => 5,
|
22
|
+
:timeout => 5000
|
23
|
+
|
24
|
+
ActiveRecord::Migrator.migrate("./spec/db/migrate")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar2gostruct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tatsuo Kaniwa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 2.3.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: Generate Go Struct from activerecord models
|
56
70
|
email:
|
57
71
|
- tatsuo@kaniwa.biz
|
@@ -64,6 +78,7 @@ files:
|
|
64
78
|
- ".gitignore"
|
65
79
|
- ".rspec"
|
66
80
|
- ".travis.yml"
|
81
|
+
- CHANGELOG.md
|
67
82
|
- Gemfile
|
68
83
|
- LICENSE.txt
|
69
84
|
- README.md
|
@@ -71,9 +86,24 @@ files:
|
|
71
86
|
- ar2gostruct.gemspec
|
72
87
|
- bin/ar2gostruct
|
73
88
|
- lib/ar2gostruct.rb
|
89
|
+
- lib/ar2gostruct/builder/association.rb
|
90
|
+
- lib/ar2gostruct/builder/orm/base.rb
|
91
|
+
- lib/ar2gostruct/builder/orm/gorm.rb
|
92
|
+
- lib/ar2gostruct/builder/orm/qbs.rb
|
93
|
+
- lib/ar2gostruct/const.rb
|
94
|
+
- lib/ar2gostruct/converter.rb
|
95
|
+
- lib/ar2gostruct/gostruct.rb
|
74
96
|
- lib/ar2gostruct/railtie.rb
|
75
97
|
- lib/ar2gostruct/railties/ar2gostruct.rake
|
76
98
|
- lib/ar2gostruct/version.rb
|
99
|
+
- spec/ar2gostruct/ar2gostruct_spec.rb
|
100
|
+
- spec/ar2gostruct/builder/association_spec.rb
|
101
|
+
- spec/ar2gostruct/builder/orm/gorm_spec.rb
|
102
|
+
- spec/ar2gostruct/builder/orm/qbs_spec.rb
|
103
|
+
- spec/db/database.yml
|
104
|
+
- spec/db/migrate/0001_create_tables.rb
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
- spec/support/models.rb
|
77
107
|
homepage: ''
|
78
108
|
licenses:
|
79
109
|
- MIT
|