goa_model_gen 0.8.3 → 0.8.4

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: 207c463abc5bf1e3b04bed480d759deb7bde42e54a8470d673ed90f3c48d3989
4
- data.tar.gz: 90bfe7129e9b8bf5bf1dd0f902d091885d1e9adb09b49a4070533d459f5bd476
3
+ metadata.gz: 35b0d93af5777f1460e792e2b1aafb7553d0bd054558a411668687d825de5e9d
4
+ data.tar.gz: 8b1ec15e0b26806474ca3005f9951529434f680e0e22b243c04d9d8467215d9f
5
5
  SHA512:
6
- metadata.gz: 47235bac3a32610001c1f2cb8c1389f5e5c66471d0cb5f6f6993ab6a7658580bfdc9d288f65ad8f6c0c159925fcd53ed65f01b56b19962f10c50a74d6d76601c
7
- data.tar.gz: 41d81eb3469f45625b1bdd60dbc963f1f7b26534dc3f6dcd83ec3f5e746104508f003b7d71a9a2e8544c5217e59a5ee92a36bab93f3a43331ca91d896acfafd6
6
+ metadata.gz: b68b7d2d1488af468dc14a78ab150d22becfcacd852aa8ec87fbf6b40b107bcaa40688154205ff3a727a6d87a7536cb3f39ca492b612188782c7ac6debd780d6
7
+ data.tar.gz: bb0dcd08ac16663d0cb3f54dfee59ed5e52d3a0a4e75872fdf2f9665e573a774d2d70ffb2de6fabd117ac337bc1eb40e73735764ca3701aa5edce74c6540bff2
@@ -88,9 +88,12 @@ module GoaModelGen
88
88
  pt = (structs['payload'] || []).detect{|t| t["Name"] == "#{m.name}Payload" }
89
89
  rt = (structs['result'] || []).detect{|t| t["Name"] == m.name }
90
90
  variables = {
91
+ models: (structs['model'] || []),
92
+ payloads: (structs['payload'] || []),
93
+ results: (structs['result'] || []),
91
94
  model: m,
92
95
  payload: pt ? GoStruct.new(pt) : nil,
93
- result: rt ? GoStruct.new(rt) : nil
96
+ result: rt ? GoStruct.new(rt) : nil,
94
97
  }
95
98
  new_generator.run('templates/converter.go.erb', File.join(cfg.converter_dir, m.name.underscore, "conv.go"), variables)
96
99
  end
@@ -6,13 +6,16 @@ def method_calling_exp(m, argument)
6
6
  "#{m}(#{argument})"
7
7
  end
8
8
  end
9
+
10
+ gen_package = "#{config.go_package}/gen/#{model.name.underscore}"
11
+
9
12
  -%>
10
13
  <%- package model.name.underscore -%>
11
14
 
12
15
  <%- if payload-%>
13
16
  <%-
14
17
  import "#{config.go_package}/converters"
15
- import :gen, "#{config.go_package}/gen/#{model.name.underscore}"
18
+ import :gen, gen_package
16
19
  import "#{config.go_package}/model"
17
20
  -%>
18
21
  func <%= model.name %>PayloadToModel(payload *gen.<%= payload.name %>) (*model.<%= model.name %>, error) {
@@ -80,9 +83,36 @@ func CopyFrom<%= model.name %>PayloadToModel(payload *gen.<%= payload.name %>, m
80
83
  <%- if result -%>
81
84
  <%-
82
85
  import "#{config.go_package}/converters"
83
- import :gen, "#{config.go_package}/gen/#{model.name.underscore}"
86
+ import :gen, gen_package
84
87
  import "#{config.go_package}/model"
88
+
89
+ indirect_types =
90
+ result.fields.map{|rf| rf.type.pkg_path == gen_package ? rf.type : nil }.
91
+ compact.uniq{|rft| "#{rft.pkg_path}.#{rft.name}" }
92
+ indirect_type_names = indirect_types.map(&:name)
93
+
94
+ indirect_types.each do |rft|
95
+ mt_hash = models.detect{|t| t['Name'] == rft.name} || raise("model not found for #{rft.name}")
96
+ rt_hash = results.detect{|rt| (rt['PkgPath'] == gen_package) && (rt['Name'] == rft.name)} || raise("#{gen_package}.#{rft.name} not found in structs")
97
+ mt = GoStruct.new(mt_hash)
98
+ rt = GoStruct.new(rt_hash)
85
99
  -%>
100
+ func <%= rt.name %>ModelToResult(m *model.<%= rt.name %>) *gen.<%= rt.name %> {
101
+ return &gen.<%= rt.name %> {
102
+ <%-
103
+ rt.fields.each do |rf|
104
+ mf = mt.fields.detect{|f| f.name == rf.name}
105
+ if mf && mf.type.assignable_with?(rf.type)
106
+ -%>
107
+ <%= rf.name %>: m.<%= mf.name %>,
108
+ <%-
109
+ end
110
+ end
111
+ -%>
112
+ }
113
+ }
114
+
115
+ <%- end -%>
86
116
  func <%= model.name %>ModelToResult(m *model.<%= model.name %>) (*gen.<%= result.name %>, error) {
87
117
  if m == nil {
88
118
  return nil, converters.NoModelGiven
@@ -103,6 +133,8 @@ func <%= model.name %>ModelToResult(m *model.<%= model.name %>) (*gen.<%= result
103
133
  r.<%= rf.name %> = m.<%= mf.name %>
104
134
  <%- elsif rf.type.pointer_of?(mf.type) -%>
105
135
  r.<%= rf.name %> = &m.<%= mf.name %>
136
+ <%- elsif indirect_type_names.include?(rf.type.name) -%>
137
+ r.<%= rf.name %> = <%= rf.type.name %>ModelToResult(&m.<%= mf.name %>)
106
138
  <%- elsif !mf.type.needs_error_to_convert_to?(rf.type) -%>
107
139
  r.<%= rf.name %> = <%= conv_pkg %>.<%= mf.type.method_part_name %>To<%= rf.type.method_part_name %>(m.<%= mf.name %>)
108
140
  <%- else -%>
@@ -1,3 +1,3 @@
1
1
  module GoaModelGen
2
- VERSION = "0.8.3"
2
+ VERSION = "0.8.4"
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.8.3
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - akm
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-05 00:00:00.000000000 Z
11
+ date: 2019-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor