ar2gostruct 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.travis.yml +12 -0
- data/Gemfile +19 -0
- data/LICENSE.txt +22 -0
- data/README.md +80 -0
- data/Rakefile +1 -0
- data/ar2gostruct.gemspec +28 -0
- data/bin/ar2gostruct +31 -0
- data/lib/ar2gostruct/railtie.rb +7 -0
- data/lib/ar2gostruct/railties/ar2gostruct.rake +4 -0
- data/lib/ar2gostruct/version.rb +3 -0
- data/lib/ar2gostruct.rb +117 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 84785b7a2fb2fca7bcafe530d8b12ba3a003c450
|
4
|
+
data.tar.gz: dd4717f5452d86065d8189801864da507be5535b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e0213301ba09195c693dadb2893516a6cfda983502de8f8d31f887755d7b820684a432904d48961a5ecb4d079d1d7e7a8046687b9d5a024c94cbdba1295c56d
|
7
|
+
data.tar.gz: f7205e9963f2a77f3e017db97fc63573bec397554a9cf11955036fcaf49384db38ef2c7d47f909cbe2cb82ae313c01c0f1407f7f31abd81308d7c248c2af3c6b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
group :development do
|
4
|
+
gem "yard"
|
5
|
+
end
|
6
|
+
|
7
|
+
group :development, :test do
|
8
|
+
gem 'simplecov'
|
9
|
+
|
10
|
+
if RUBY_PLATFORM =~ /darwin/
|
11
|
+
gem "ruby_gntp"
|
12
|
+
gem "rb-fsevent"
|
13
|
+
end
|
14
|
+
if RUBY_PLATFORM =~ /linux/
|
15
|
+
gem 'libnotify'
|
16
|
+
gem 'rb-inotify'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
gemspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Tatsuo Kaniwa
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# Ar2gostruct
|
2
|
+
|
3
|
+
Automatically generate Golang Struct from your activerecord models.
|
4
|
+
|
5
|
+
Installation
|
6
|
+
---
|
7
|
+
|
8
|
+
Install GMO as a gem:
|
9
|
+
```bash
|
10
|
+
gem install ar2gostruct
|
11
|
+
```
|
12
|
+
|
13
|
+
or add to your Gemfile:
|
14
|
+
```ruby
|
15
|
+
# Gemfile
|
16
|
+
gem "ar2gostruct"
|
17
|
+
```
|
18
|
+
and run bundle install to install the dependency.
|
19
|
+
|
20
|
+
Usage
|
21
|
+
---
|
22
|
+
|
23
|
+
On your rails directory.
|
24
|
+
```bash
|
25
|
+
bundle exec ar2gostruct
|
26
|
+
```
|
27
|
+
this will returns
|
28
|
+
```bash
|
29
|
+
// app/models/users.rb
|
30
|
+
// Table name: users
|
31
|
+
type Users struct {
|
32
|
+
Id int32 'json:"id"'
|
33
|
+
Email string 'json:"email"'
|
34
|
+
EncryptedPassword string 'json:"encrypted_password"'
|
35
|
+
ResetPasswordToken string 'json:"reset_password_token"'
|
36
|
+
ResetPasswordSentAt time.Time 'json:"reset_password_sent_at"'
|
37
|
+
RememberCreatedAt time.Time 'json:"remember_created_at"'
|
38
|
+
SignInCount int32 'json:"sign_in_count"'
|
39
|
+
CurrentSignInAt time.Time 'json:"current_sign_in_at"'
|
40
|
+
LastSignInAt time.Time 'json:"last_sign_in_at"'
|
41
|
+
CurrentSignInIp string 'json:"current_sign_in_ip"'
|
42
|
+
LastSignInIp string 'json:"last_sign_in_ip"'
|
43
|
+
CreatedAt time.Time 'json:"created_at"'
|
44
|
+
UpdatedAt time.Time 'json:"updated_at"'
|
45
|
+
}
|
46
|
+
```
|
47
|
+
|
48
|
+
If you're using [qbs](https://github.com/coocood/qbs#), Additional options are available.
|
49
|
+
|
50
|
+
```bash
|
51
|
+
bundle exec ar2gostruct -o qbs
|
52
|
+
|
53
|
+
// app/models/users.rb
|
54
|
+
// Table name: users
|
55
|
+
type Users struct {
|
56
|
+
Id int32 'json:"id" qbs:"pk,notnull"'
|
57
|
+
Email string 'json:"email" qbs:"notnull,default:''"'
|
58
|
+
EncryptedPassword string 'json:"encrypted_password" qbs:"notnull,default:''"'
|
59
|
+
ResetPasswordToken string 'json:"reset_password_token"'
|
60
|
+
ResetPasswordSentAt time.Time 'json:"reset_password_sent_at"'
|
61
|
+
RememberCreatedAt time.Time 'json:"remember_created_at"'
|
62
|
+
SignInCount int32 'json:"sign_in_count" qbs:"default:'0'"'
|
63
|
+
CurrentSignInAt time.Time 'json:"current_sign_in_at"'
|
64
|
+
LastSignInAt time.Time 'json:"last_sign_in_at"'
|
65
|
+
CurrentSignInIp string 'json:"current_sign_in_ip"'
|
66
|
+
LastSignInIp string 'json:"last_sign_in_ip"'
|
67
|
+
CreatedAt time.Time 'json:"created_at"'
|
68
|
+
UpdatedAt time.Time 'json:"updated_at"'
|
69
|
+
}
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
Contributing
|
74
|
+
---
|
75
|
+
|
76
|
+
1. Fork it
|
77
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
78
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
79
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
80
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/ar2gostruct.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ar2gostruct/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ar2gostruct"
|
8
|
+
spec.version = Ar2gostruct::VERSION
|
9
|
+
spec.authors = ["Tatsuo Kaniwa"]
|
10
|
+
spec.email = ["tatsuo@kaniwa.biz"]
|
11
|
+
spec.description = %q{Generate Go Struct from activerecord models}
|
12
|
+
spec.summary = %q{Generate Go Struct from activerecord models}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.files += Dir.glob("{lib}/**/*")
|
18
|
+
spec.executables << 'ar2gostruct'
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
spec.test_files = `git ls-files -- {spec}/*`.split("\n")
|
21
|
+
|
22
|
+
spec.extra_rdoc_files = ["README.md"]
|
23
|
+
spec.rdoc_options = ["--line-numbers", "--inline-source", "--title", "ar2gostruct"]
|
24
|
+
|
25
|
+
spec.add_dependency "rake", ">= 0.8.7"
|
26
|
+
spec.add_dependency "activerecord", ">= 2.3.0"
|
27
|
+
spec.add_dependency "activesupport", ">= 2.3.0"
|
28
|
+
end
|
data/bin/ar2gostruct
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
begin
|
4
|
+
require 'bundler'
|
5
|
+
Bundler.setup(:default)
|
6
|
+
rescue Exception => e
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'optparse'
|
10
|
+
require 'ar2gostruct'
|
11
|
+
|
12
|
+
OptionParser.new do |opt|
|
13
|
+
opt.banner = "Usage: annotate [options] [model_file]*"
|
14
|
+
|
15
|
+
opt.on('-v', '--version',
|
16
|
+
"Show the current version of this gem") do
|
17
|
+
puts "ar2gostruct v#{Ar2gostruct.version}"; exit
|
18
|
+
end
|
19
|
+
|
20
|
+
opt.on('--orm orm', "Specify ORM. Currently supports only qbs") do |orm|
|
21
|
+
ENV['orm'] = orm.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
opt.on('--model-dir dir',
|
25
|
+
"Model files stored in dir rather than app/models") do |dir|
|
26
|
+
ENV['model_dir'] = dir.to_s
|
27
|
+
end
|
28
|
+
opt.parse!(ARGV)
|
29
|
+
end
|
30
|
+
Ar2gostruct.load
|
31
|
+
Ar2gostruct.convert!
|
data/lib/ar2gostruct.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
require "ar2gostruct/version"
|
2
|
+
|
3
|
+
require "active_support/inflector"
|
4
|
+
require "active_record"
|
5
|
+
|
6
|
+
require "ar2gostruct/railtie"
|
7
|
+
|
8
|
+
module Ar2gostruct
|
9
|
+
MODEL_DIR = ENV["model_dir"] || "app/models"
|
10
|
+
|
11
|
+
TYPE_MAP = {
|
12
|
+
"string" => "string",
|
13
|
+
"text" => "string",
|
14
|
+
"boolean" => "bool",
|
15
|
+
"integer(1)" => "int8",
|
16
|
+
"integer(2)" => "int16",
|
17
|
+
"integer(3)" => "int32",
|
18
|
+
"integer(4)" => "int32",
|
19
|
+
"integer(8)" => "int64",
|
20
|
+
"float" => "float64",
|
21
|
+
"datetime" => "time.Time",
|
22
|
+
"date" => "time.Time"
|
23
|
+
}
|
24
|
+
|
25
|
+
def self.load
|
26
|
+
begin
|
27
|
+
require "#{Dir.pwd}/config/environment"
|
28
|
+
rescue
|
29
|
+
end
|
30
|
+
Rails.application.eager_load!
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.get_schema_info(klass)
|
34
|
+
info = "// Table name: #{klass.table_name}\n"
|
35
|
+
info << "type #{klass.table_name.camelize} struct {\n"
|
36
|
+
|
37
|
+
max_size = klass.column_names.collect{|name| name.size}.max + 1
|
38
|
+
klass.columns.each do |col|
|
39
|
+
tags = []
|
40
|
+
|
41
|
+
# add json tag
|
42
|
+
tags << "json:\"#{col.name}\""
|
43
|
+
|
44
|
+
case ENV["orm"]
|
45
|
+
when "qbs"
|
46
|
+
orm_option = []
|
47
|
+
# primary key
|
48
|
+
if col.name == klass.primary_key
|
49
|
+
orm_option << "pk"
|
50
|
+
end
|
51
|
+
# not null Constraint
|
52
|
+
unless col.null
|
53
|
+
orm_option << "notnull"
|
54
|
+
end
|
55
|
+
# default value
|
56
|
+
if col.default
|
57
|
+
orm_option << "default:'#{col.default}'"
|
58
|
+
end
|
59
|
+
if orm_option.present?
|
60
|
+
tags << "qbs:\"#{orm_option.join(",")}\""
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
col_type = col.type.to_s
|
65
|
+
case col_type
|
66
|
+
when "integer"
|
67
|
+
type = TYPE_MAP["integer(#{col.limit})"] || "int32"
|
68
|
+
type = "u#{col_type}" if col.sql_type.match("unsigned").present?
|
69
|
+
else
|
70
|
+
type = TYPE_MAP[col_type] || "string"
|
71
|
+
end
|
72
|
+
|
73
|
+
info << sprintf("\t%-#{max_size}.#{max_size}s%-15.15s'%s'\n", col.name.camelize, type, tags.join(" "))
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
info << "}\n\n"
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.convert_to_gostruct(klass)
|
81
|
+
info = get_schema_info(klass)
|
82
|
+
|
83
|
+
model_file_name = File.join(MODEL_DIR, klass.name.underscore + ".rb")
|
84
|
+
|
85
|
+
puts "// #{model_file_name}"
|
86
|
+
puts info
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.get_model_names
|
90
|
+
models = ARGV.dup
|
91
|
+
models.shift
|
92
|
+
|
93
|
+
if models.empty?
|
94
|
+
Dir.chdir(MODEL_DIR) do
|
95
|
+
models = Dir["**/*.rb"]
|
96
|
+
end
|
97
|
+
end
|
98
|
+
models
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.convert!
|
102
|
+
annotated = []
|
103
|
+
self.get_model_names.each do |m|
|
104
|
+
class_name = m.sub(/\.rb$/,'').camelize
|
105
|
+
begin
|
106
|
+
klass = class_name.split('::').inject(Object){ |klass,part| klass.const_get(part) }
|
107
|
+
if klass < ActiveRecord::Base && !klass.abstract_class?
|
108
|
+
annotated << class_name
|
109
|
+
self.convert_to_gostruct(klass)
|
110
|
+
end
|
111
|
+
rescue Exception => e
|
112
|
+
puts "// Unable to annotate #{class_name}: #{e.message}"
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ar2gostruct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tatsuo Kaniwa
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.8.7
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.8.7
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.3.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.3.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.3.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.3.0
|
55
|
+
description: Generate Go Struct from activerecord models
|
56
|
+
email:
|
57
|
+
- tatsuo@kaniwa.biz
|
58
|
+
executables:
|
59
|
+
- ar2gostruct
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files:
|
62
|
+
- README.md
|
63
|
+
files:
|
64
|
+
- .gitignore
|
65
|
+
- .rspec
|
66
|
+
- .travis.yml
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- ar2gostruct.gemspec
|
72
|
+
- bin/ar2gostruct
|
73
|
+
- lib/ar2gostruct.rb
|
74
|
+
- lib/ar2gostruct/railtie.rb
|
75
|
+
- lib/ar2gostruct/railties/ar2gostruct.rake
|
76
|
+
- lib/ar2gostruct/version.rb
|
77
|
+
homepage: ''
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options:
|
83
|
+
- --line-numbers
|
84
|
+
- --inline-source
|
85
|
+
- --title
|
86
|
+
- ar2gostruct
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.0.3
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: Generate Go Struct from activerecord models
|
105
|
+
test_files: []
|
106
|
+
has_rdoc:
|