goa_model_gen 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 10b556d055acc2054efa01a18fa0610dea43bdfa88be354ce818807cdd594a21
4
- data.tar.gz: e456994ddafd3e12112af08d88bb797326b951ec979d15844187882f6e95eb82
3
+ metadata.gz: a94a826ba73aaaf70ac461966a30335bd2172b4b21dc8b2a9cdb67b900c18adb
4
+ data.tar.gz: c98e3ae9d9e57a0c45bd2cc5fcf8452ae40de3957a7813cb1fc8ba67ce0ff874
5
5
  SHA512:
6
- metadata.gz: 1d9e5251f038931fd7381e0fe2520e0e10b9c79ccd5a699697878d2fc2d9721ea10cf49aca80e8d3ea4d42b455844b2d2e2e3aca9585053addda0cb7d74ad245
7
- data.tar.gz: c2dcd3a32e11d48cb97be59d75fd12aa751192414bc64aa4b54ccbb167f0885e7c5c69d3eb48b029348805b5fb27bf269033f0e1879fee4f0024193cd90e011d
6
+ metadata.gz: 5d4f762066e7d4d2f797a159041833918f955107b51fcb39f52b02050e2eb9252fdc1e594aafc22d30c8d7c8da5aecee58c31df66d522c6f194d52ddf94e6b45
7
+ data.tar.gz: 8c00b816ebd1805f36f497e9e336794abb403d57dbf5a9282cd6f461246ca8a05bec0b8ec4ee0192ce837f31d3a17cbe3f8a83cdb8a22be8cc33a43a3bcd14cf
@@ -1,59 +1,94 @@
1
1
  require "goa_model_gen"
2
2
 
3
- require "erb"
4
-
5
3
  require "thor"
6
4
 
5
+ require "goa_model_gen/config"
7
6
  require "goa_model_gen/loader"
8
7
  require "goa_model_gen/generator"
9
8
 
10
9
  module GoaModelGen
11
10
  class Cli < Thor
12
- class_option :swagger_yaml, type: :string, default: './swagger/swagger.yaml', desc: 'Swagger definition YAML file'
11
+ class_option :version, type: :boolean, aliases: 'v', desc: 'Show version before processing'
12
+ class_option :config, type: :string, aliases: 'c', default: './goa_model_gen.yaml', desc: 'Path to config file. You can generate it by config subcommand'
13
+
14
+ desc "config", "Generate config file"
15
+ def config(path = './goa_model_gen.yaml')
16
+ show_version_if_required
17
+ open(path, 'w'){|f| f.puts(Config.new.fulfill.to_yaml) }
18
+ end
19
+
20
+ desc "bootstrap", "Generate files not concerned with model"
21
+ def bootstrap
22
+ show_version_if_required
23
+ generator = new_generator
24
+ {
25
+ "templates/goon.go.erb" => "model/goon.go",
26
+ "templates/converter_base.go.erb" => "controller/converter_base.go",
27
+ }.each do |template, dest|
28
+ generator.run(template, dest, overwrite: true)
29
+ end
30
+ end
13
31
 
14
32
  desc "show FILE1...", "Show model info from definition files"
15
33
  def show(*paths)
16
- load_types_for(paths, options[:swagger_yaml]) do |path, types|
34
+ show_version_if_required
35
+ load_types_for(paths) do |path, types|
17
36
  puts "types in #{path}"
18
37
  puts YAML.dump(types)
19
38
  end
20
39
  end
21
40
 
22
41
  desc "model FILE1...", "Generate model files from definition files"
23
- option :dir, type: :string, default: './model', desc: 'Output directory path'
24
- option :gofmt, type: :boolean, default: true, desc: 'Run gofmt for generated file'
25
42
  def model(*paths)
26
- generator = GoaModelGen::Generator.new(File.expand_path('../templates/model.go.erb', __FILE__))
27
- load_types_for(paths, options[:swagger_yaml]) do |path, types|
28
- dest = File.join(options[:dir], File.basename(path, ".*") + ".go")
29
- generator.run(types, dest)
30
- if options[:gofmt]
31
- system("gofmt -w #{dest}")
43
+ show_version_if_required
44
+ load_types_for(paths) do |path, types|
45
+ generator = new_generator.tap{|g| g.types = types }
46
+ [
47
+ {path: 'templates/model.go.erb', suffix: '.go', overwrite: true},
48
+ {path: 'templates/model_validation.go.erb', suffix: '_validation.go', overwrite: false},
49
+ ].each do |d|
50
+ dest = File.join(cfg.model_dir, File.basename(path, ".*") + d[:suffix])
51
+ generator.run(d[:path], dest, overwrite: d[:overwrite])
32
52
  end
33
53
  end
34
54
  end
35
55
 
36
56
  desc "converter FILE1...", "Generate converter files from definition files and swagger.yaml"
37
- option :package, type: :string, default: 'controller', desc: 'package name'
38
- option :dir, type: :string, default: './controller', desc: 'Output directory path'
39
- option :gofmt, type: :boolean, default: true, desc: 'Run gofmt for generated file'
40
57
  def converter(*paths)
41
- generator = GoaModelGen::Generator.new(File.expand_path('../templates/converter.go.erb', __FILE__))
42
- load_types_for(paths, options[:swagger_yaml]) do |path, types|
43
- dest = File.join(options[:dir], File.basename(path, ".*") + "_conv.go")
58
+ show_version_if_required
59
+ load_types_for(paths) do |path, types|
60
+ generator = new_generator.tap{|g| g.types = types }
61
+ dest = File.join(cfg.controller_dir, File.basename(path, ".*") + "_conv.go")
44
62
  if types.any?{|t| !!t.payload || !!t.media_type}
45
- generator.run(types, dest)
46
- if options[:gofmt]
47
- system("gofmt -w #{dest}")
48
- end
63
+ generator.run('templates/converter.go.erb', dest, overwrite: true)
49
64
  end
50
65
  end
51
66
  end
52
67
 
68
+ desc "version", "Show version"
69
+ def version
70
+ show_version
71
+ end
72
+
53
73
  no_commands do
74
+ def show_version_if_required
75
+ show_version if options[:version]
76
+ end
77
+
78
+ def show_version
79
+ puts "#{$PROGRAM_NAME} version:#{::GoaModelGen::VERSION}"
80
+ end
81
+
82
+ def cfg
83
+ @cfg ||= GoaModelGen::Config.new.load_from(options[:config])
84
+ end
85
+
86
+ def new_generator
87
+ GoaModelGen::Generator.new(cfg)
88
+ end
54
89
 
55
- def load_types_for(paths, swagger_yaml)
56
- swagger_loader = GoaModelGen::SwaggerLoader.new(swagger_yaml)
90
+ def load_types_for(paths)
91
+ swagger_loader = GoaModelGen::SwaggerLoader.new(cfg.swagger_yaml)
57
92
  path_to_types = {}
58
93
  defined_types = {}
59
94
  paths.each do |path|
@@ -0,0 +1,66 @@
1
+ require "goa_model_gen"
2
+
3
+ require "erb"
4
+ require "yaml"
5
+ require "pathname"
6
+
7
+ require "active_support/core_ext/string"
8
+
9
+ module GoaModelGen
10
+ class Config
11
+
12
+ ATTRIBUTES = %w[
13
+ go_package
14
+ swagger_yaml
15
+ gofmt_disabled
16
+ model_dir
17
+ controller_dir
18
+ ].freeze
19
+
20
+ attr_reader *ATTRIBUTES
21
+
22
+ def fulfill
23
+ @go_package ||= default_go_package
24
+ @swagger_yaml ||= "./swagger/swagger.yaml"
25
+ @gofmt_disabled ||= false
26
+ @model_dir ||= "./model"
27
+ @controller_dir ||= "./controller"
28
+ self
29
+ end
30
+
31
+ def load_from(path)
32
+ erb = ERB.new(File.read(path), nil, "-")
33
+ erb.filename = path
34
+ config = YAML.load(erb.result, path)
35
+
36
+ ATTRIBUTES.each do |name|
37
+ instance_variable_set("@#{name}", config[name].presence)
38
+ end
39
+
40
+ fulfill
41
+ end
42
+
43
+ def to_hash
44
+ ATTRIBUTES.each_with_object({}) do |name, d|
45
+ d[name] = send(name)
46
+ end
47
+ end
48
+
49
+ def to_yaml
50
+ YAML.dump(to_hash)
51
+ end
52
+
53
+ def default_go_package
54
+ return default_go_package!
55
+ rescue
56
+ nil
57
+ end
58
+
59
+ def default_go_package!
60
+ gopath = ENV['GOPATH'] || ''
61
+ raise "$GOPATH not found" if gopath.empty?
62
+ return Pathname.new(Dir.pwd).relative_path_from(Pathname.new(File.join(gopath, "src"))).to_s
63
+ end
64
+
65
+ end
66
+ end
@@ -7,15 +7,24 @@ require "active_support/core_ext/string"
7
7
 
8
8
  module GoaModelGen
9
9
  class Generator
10
- def initialize(template_path)
11
- content = File
12
- @erb = ERB.new(File.read(template_path), nil, "-")
13
- @erb.filename = template_path
10
+ # These are used in templates
11
+ attr_reader :config
12
+ attr_accessor :types
13
+
14
+ def initialize(config)
15
+ @config = config
14
16
  end
15
17
 
16
- def run(types, path)
17
- content = @erb.result(binding)
18
+ def run(rel_path, path, overwrite: false)
19
+ return if File.exist?(path) && !overwrite
20
+ abs_path = File.expand_path('../' + rel_path, __FILE__)
21
+ erb = ERB.new(File.read(abs_path), nil, "-")
22
+ erb.filename = abs_path
23
+ content = erb.result(binding)
18
24
  open(path, 'w'){|f| f.puts(content) }
25
+ if (File.extname(path) == '.go') && !config.gofmt_disabled
26
+ system("gofmt -w #{path}")
27
+ end
19
28
  end
20
29
 
21
30
  end
@@ -1,8 +1,8 @@
1
1
  package controller
2
2
 
3
3
  import (
4
- "github.com/groovenauts/blocks-concurrent-batch-server/app"
5
- "github.com/groovenauts/blocks-concurrent-batch-server/model"
4
+ "<%= config.go_package %>/app"
5
+ "<%= config.go_package %>/model"
6
6
  )
7
7
 
8
8
  <%- types.select(&:gen_converter?).each do |type| -%>
@@ -0,0 +1,60 @@
1
+ package controller
2
+
3
+ import (
4
+ "strconv"
5
+ )
6
+
7
+ func BoolPointerToBool(v *bool) bool {
8
+ return BoolPointerToBoolWith(v, false)
9
+ }
10
+
11
+ func BoolPointerToBoolWith(v *bool, d bool) bool {
12
+ if v == nil {
13
+ return d
14
+ }
15
+ return *v
16
+ }
17
+
18
+ func IntPointerToInt(v *int) int {
19
+ return IntPointerToIntWith(v, 0)
20
+ }
21
+
22
+ func IntPointerToIntWith(v *int, d int) int {
23
+ if v == nil {
24
+ return d
25
+ }
26
+ return *v
27
+ }
28
+
29
+ func IntToInt64(v int) int64 {
30
+ return int64(v)
31
+ }
32
+
33
+ func IntToInt64Pointer(v int) *int64 {
34
+ r := IntToInt64(v)
35
+ return &r
36
+ }
37
+
38
+ func StringPointerToString(v *string) string {
39
+ return StringPointerToStringWith(v, "")
40
+ }
41
+
42
+ func StringPointerToStringWith(v *string, d string) string {
43
+ if v == nil {
44
+ return d
45
+ }
46
+ return *v
47
+ }
48
+
49
+ func StringToInt64(v string) (int64, error) {
50
+ return strconv.ParseInt(v, 10, 64)
51
+ }
52
+
53
+ func Int64ToString(v int64) string {
54
+ return strconv.FormatInt(v, 10)
55
+ }
56
+
57
+ func Int64ToStringPointer(v int64) *string {
58
+ s := Int64ToString(v)
59
+ return &s
60
+ }
@@ -0,0 +1,27 @@
1
+ package model
2
+
3
+ import (
4
+ "golang.org/x/net/context"
5
+
6
+ "github.com/mjibson/goon"
7
+ )
8
+
9
+ // // Use the following code if you want to change kind name from model struct name.
10
+ // var ModelNameToKindMap = map[string]string{
11
+ // "Person": "People",
12
+ // "Book": "Books",
13
+ // }
14
+
15
+ func GoonFromContext(c context.Context) *goon.Goon {
16
+ r := goon.FromContext(c)
17
+ // baseResolver := r.KindNameResolver
18
+ // r.KindNameResolver = func(src interface{}) string {
19
+ // base := baseResolver(src)
20
+ // mapped := ModelNameToKindMap[base]
21
+ // if mapped != "" {
22
+ // return mapped
23
+ // }
24
+ // return base
25
+ // }
26
+ return r
27
+ }
@@ -196,7 +196,7 @@ func (s *<%= store_name %>) Create(ctx context.Context, m *<%= model.name %>) (*
196
196
  }
197
197
  if exist {
198
198
  log.Errorf(ctx, "Failed to create %v because of another entity has same key\n", m)
199
- return nil, fmt.Errorf("Duplicate Name error: %q of %v\n", m.Name, m)
199
+ return nil, fmt.Errorf("Duplicate <%= model.goon['id_name'] %> error: %q of %v\n", m.<%= model.goon['id_name'] %>, m)
200
200
  }
201
201
  <%- end -%>
202
202
 
@@ -219,7 +219,7 @@ func (s *<%= store_name %>) Update(ctx context.Context, m *<%= model.name %>) (*
219
219
  }
220
220
  if !exist {
221
221
  log.Errorf(ctx, "Failed to update %v because it doesn't exist\n", m)
222
- return nil, fmt.Errorf("No data to update %q of %v\n", m.Name, m)
222
+ return nil, fmt.Errorf("No data to update %q of %v\n", m.<%= model.goon['id_name'] %>, m)
223
223
  }
224
224
  <%- end -%>
225
225
 
@@ -0,0 +1,13 @@
1
+ package model
2
+
3
+ import (
4
+ "gopkg.in/go-playground/validator.v9"
5
+ )
6
+
7
+ <%- types.select(&:store?).each do |model| -%>
8
+ func (m *<%= model.name %>) Validate() error {
9
+ validator := validator.New()
10
+ return validator.Struct(m)
11
+ }
12
+
13
+ <%- end -%>
@@ -1,3 +1,3 @@
1
1
  module GoaModelGen
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: goa_model_gen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - akm
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-17 00:00:00.000000000 Z
11
+ date: 2018-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -116,12 +116,16 @@ files:
116
116
  - goa_model_gen.gemspec
117
117
  - lib/goa_model_gen.rb
118
118
  - lib/goa_model_gen/cli.rb
119
+ - lib/goa_model_gen/config.rb
119
120
  - lib/goa_model_gen/field.rb
120
121
  - lib/goa_model_gen/generator.rb
121
122
  - lib/goa_model_gen/goa.rb
122
123
  - lib/goa_model_gen/loader.rb
123
124
  - lib/goa_model_gen/templates/converter.go.erb
125
+ - lib/goa_model_gen/templates/converter_base.go.erb
126
+ - lib/goa_model_gen/templates/goon.go.erb
124
127
  - lib/goa_model_gen/templates/model.go.erb
128
+ - lib/goa_model_gen/templates/model_validation.go.erb
125
129
  - lib/goa_model_gen/type.rb
126
130
  - lib/goa_model_gen/version.rb
127
131
  homepage: https://github.com/akm/goa_model_gen