goa_model_gen 0.8.2 → 0.8.3

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: cca554d53fda64080cd5a64e2408b2558f548c5d6acd6f5e14848736cf2d39fa
4
- data.tar.gz: a426126a914b0b8d3e70f8fe6e4e995540c099dcbe5eadb97705ba489a146ceb
3
+ metadata.gz: 207c463abc5bf1e3b04bed480d759deb7bde42e54a8470d673ed90f3c48d3989
4
+ data.tar.gz: 90bfe7129e9b8bf5bf1dd0f902d091885d1e9adb09b49a4070533d459f5bd476
5
5
  SHA512:
6
- metadata.gz: 890c57ae95750b80d5b96034c4c4844e7660c027c2e3959e53de6491bfb5f2d742922753a1ad1f7be55b28330fab3c34c650341b0053e8f9a602b56fd9608c38
7
- data.tar.gz: 4c11251847cc608ec123875924ef01393f0d8c95e66dedf3cb03491c2cb18efe85a9c06cd79288739acbcd37b30d335a878ba8b3c6d6949547d58f56afd818b7
6
+ metadata.gz: 47235bac3a32610001c1f2cb8c1389f5e5c66471d0cb5f6f6993ab6a7658580bfdc9d288f65ad8f6c0c159925fcd53ed65f01b56b19962f10c50a74d6d76601c
7
+ data.tar.gz: 41d81eb3469f45625b1bdd60dbc963f1f7b26534dc3f6dcd83ec3f5e746104508f003b7d71a9a2e8544c5217e59a5ee92a36bab93f3a43331ca91d896acfafd6
@@ -43,6 +43,7 @@ module GoaModelGen
43
43
  setup
44
44
  new_generator.process({
45
45
  'templates/validator.go.erb' => File.join(cfg.model_dir, 'validator.go'),
46
+ 'templates/model_base.go.erb' => File.join(cfg.model_dir, "model_base.go"),
46
47
  })
47
48
  load_types_for(paths) do |source_file|
48
49
  new_generator.tap{|g| g.source_file = source_file }.process({
@@ -43,6 +43,9 @@ module GoaModelGen
43
43
  end
44
44
  alias_method :assignable_with?, :==
45
45
 
46
+ def pointer?
47
+ kinds.last == 'ptr'
48
+ end
46
49
  def pointer_of?(other)
47
50
  (pkg_path == other.pkg_path) &&
48
51
  (name == other.name) &&
@@ -59,10 +62,16 @@ module GoaModelGen
59
62
  end
60
63
 
61
64
  def method_part_name
65
+ parts = kinds.dup
66
+ parts.pop if parts.last == 'ptr'
62
67
  parts =
63
- kinds.first == 'struct' ?
64
- [name] + kinds[1..-1] : kinds
68
+ (parts.first == 'struct' || pkg_path.present?) ?
69
+ [name] + parts[1..-1] : parts
65
70
  parts.map(&:camelize).join
66
71
  end
72
+
73
+ def defined_in_model?
74
+ pkg_path =~ /\/model\z/
75
+ end
67
76
  end
68
77
  end
@@ -37,6 +37,7 @@ func CopyFrom<%= model.name %>PayloadToModel(payload *gen.<%= payload.name %>, m
37
37
  pf_name = (mf.tags['payload'] || mf.name).downcase
38
38
  pf = payload.fields.detect{|pf| pf.name.downcase == pf_name }
39
39
  assigned_field_names.push(pf.name) if pf
40
+ conv_pkg = mf.type.defined_in_model? ? 'model' : 'converters'
40
41
  if pf.nil?
41
42
  -%>
42
43
  // <%= mf.name %> not found in <%= payload.name %> fields
@@ -47,13 +48,20 @@ func CopyFrom<%= model.name %>PayloadToModel(payload *gen.<%= payload.name %>, m
47
48
  m.<%= mf.name %> = *payload.<%= pf.name %>
48
49
  }
49
50
  <%- elsif !pf.type.needs_error_to_convert_to?(mf.type) -%>
50
- m.<%= mf.name %> = converters.<%= pf.type.method_part_name %>To<%= mf.type.method_part_name %>(payload.<%= pf.name %>)
51
+ m.<%= mf.name %> = <%= conv_pkg %>.<%= pf.type.method_part_name %>To<%= mf.type.method_part_name %>(payload.<%= pf.name %>)
51
52
  <%- else -%>
52
- if v, err := converters.<%= pf.type.method_part_name %>To<%= mf.type.method_part_name %>(payload.<%= pf.name %>); err != nil {
53
+ <%- meth = "#{pf.type.method_part_name}To#{mf.type.method_part_name}" -%>
54
+ <%- if pf.type.pointer? -%>
55
+ if payload.<%= pf.name %> != nil {
56
+ <%- end -%>
57
+ if v, err := <%= conv_pkg %>.<%= meth %>(<%= '*' if pf.type.pointer? %>payload.<%= pf.name %>); err != nil {
53
58
  return err
54
59
  } else {
55
60
  m.<%= mf.name %> = v
56
61
  }
62
+ <%- if pf.type.pointer? -%>
63
+ }
64
+ <%- end -%>
57
65
  <%- end -%>
58
66
  <%- end -%>
59
67
  <%-
@@ -87,6 +95,7 @@ func <%= model.name %>ModelToResult(m *model.<%= model.name %>) (*gen.<%= result
87
95
  rf_name = (mf.tags['result'] || mf.name).downcase
88
96
  rf = result.fields.detect{|rf| rf.name.downcase == rf_name }
89
97
  assigned_field_names.push(rf.name) if rf
98
+ conv_pkg = mf.type.defined_in_model? ? 'model' : 'converters'
90
99
  if rf.nil?
91
100
  -%>
92
101
  // <%= mf.name %> not found in <%= result.name %> fields
@@ -95,9 +104,10 @@ func <%= model.name %>ModelToResult(m *model.<%= model.name %>) (*gen.<%= result
95
104
  <%- elsif rf.type.pointer_of?(mf.type) -%>
96
105
  r.<%= rf.name %> = &m.<%= mf.name %>
97
106
  <%- elsif !mf.type.needs_error_to_convert_to?(rf.type) -%>
98
- r.<%= rf.name %> = converters.<%= mf.type.method_part_name %>To<%= rf.type.method_part_name %>(m.<%= mf.name %>)
107
+ r.<%= rf.name %> = <%= conv_pkg %>.<%= mf.type.method_part_name %>To<%= rf.type.method_part_name %>(m.<%= mf.name %>)
99
108
  <%- else -%>
100
- if v, err := converters.<%= mf.type.method_part_name %>To<%= rf.type.method_part_name %>(m.<%= mf.name %>); err != nil {
109
+ <%- meth = "#{mf.type.method_part_name}To#{rf.type.method_part_name}" -%>
110
+ if v, err := <%= conv_pkg %>.<%= meth %>(m.<%= mf.name %>); err != nil {
101
111
  return err
102
112
  } else {
103
113
  r.<%= rf.name %> = v
@@ -53,15 +53,21 @@ var <%= type.name %>NameToValue = map[string]<%= type.name %>{
53
53
  <%- end -%>
54
54
  }
55
55
 
56
- func Parse<%= type.name %>(s string) *<%= type.name %> {
56
+ func StringTo<%= type.name %>(s string) (<%= type.name %>, error) {
57
57
  val, ok := <%= type.name %>NameToValue[s]
58
58
  if ok {
59
- return &val
59
+ return val, nil
60
60
  } else {
61
- return nil
61
+ err := &InvalidEnumName{Name: s}
62
+ return <%= type.name %><%= type.enum_map[type.enum_map.keys.first].camelize %>, err
62
63
  }
63
64
  }
64
65
 
66
+
67
+ func <%= type.name %>ToString(v <%= type.name %>) string {
68
+ return v.String()
69
+ }
70
+
65
71
  <%- end -%>
66
72
 
67
73
  <%- end -%>
@@ -0,0 +1,15 @@
1
+ <%- user_editable -%>
2
+ <%- package "model" -%>
3
+
4
+ <%-
5
+ import "fmt"
6
+ -%>
7
+
8
+ type InvalidEnumName struct {
9
+ Name string
10
+ // Options []string
11
+ }
12
+
13
+ func (e *InvalidEnumName) Error() string {
14
+ return fmt.Sprintf("Invalid enum name %q", e.Name)
15
+ }
@@ -1,3 +1,3 @@
1
1
  module GoaModelGen
2
- VERSION = "0.8.2"
2
+ VERSION = "0.8.3"
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.2
4
+ version: 0.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - akm
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-30 00:00:00.000000000 Z
11
+ date: 2019-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -158,6 +158,7 @@ files:
158
158
  - lib/goa_model_gen/templates/converter_base.go.erb
159
159
  - lib/goa_model_gen/templates/goon.go.erb
160
160
  - lib/goa_model_gen/templates/model.go.erb
161
+ - lib/goa_model_gen/templates/model_base.go.erb
161
162
  - lib/goa_model_gen/templates/model_validation.go.erb
162
163
  - lib/goa_model_gen/templates/store.go.erb
163
164
  - lib/goa_model_gen/templates/store_validation.go.erb