goon_model_gen 0.2.1 → 0.2.2

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: ae4ea6261885660b6956bce4d82aaa078edd6d727ab6a4e144568473cfdaed69
4
- data.tar.gz: 3dbfdd5ac21c79fc6adf1bb9009d81de2584327b53f0c92bcaf4a1150dcc623c
3
+ metadata.gz: dcbd8191e356ee177b06bfbe3eac365ea41a17f2c112bffaeec6acffde03cc80
4
+ data.tar.gz: fbac547b17220f58c43edbeeb209b7b0a5db80b5dca434f56c1a1f7b14137102
5
5
  SHA512:
6
- metadata.gz: 515e715aaffaf9b359ede38fda8b35c12f9cb75eb7ef8fec5be2318175d9ec75233f32ef77e0200b2351302d9fbba9ebc2b6261eddc705a26605a049d07bdc3b
7
- data.tar.gz: 4f8e860ed5ca58cbd6d7f8f1a7b96dc8cd98548426a1b6ae47679d27894425b5222b1c645caaa830d524f6cae11f56c4ecb3d76b559c25058973f87433b34ef5
6
+ metadata.gz: 1fe22afe60a09fa65c67d80bc589118a43dfdfb5140057c7643a4dac3e5de568096de8a9d8a2094443d86c84c7157fcbd806ed40bc913d79b66f7120dded5ceb
7
+ data.tar.gz: 28a70927a80f1a92ad9ae23f0dc5b17a7172091c27658fc4e7fa04ad53bb955be38fd294175bc476111ff7201ee21a357433756ce7a599d051a713992ae75114
@@ -29,7 +29,9 @@ module GoonModelGen
29
29
  kind = (t.id_name && t.id_type) ? 'goon' : 'struct'
30
30
  procs << Proc.new{ build_sentences('model', kind, t, go_type) }
31
31
  when Source::Enum then
32
- go_type = pkg.new_enum(t.name, t.base_type, t.map)
32
+ go_type = pkg.new_enum(t.name, t.base_type).tap do |enum|
33
+ t.elements.each{|i| enum.add(i.value, i.name) }
34
+ end
33
35
  procs << Proc.new{ build_sentences('model', 'enum', t, go_type) }
34
36
  when Source::NamedSlice then
35
37
  go_type = pkg.new_named_slice(t.name, t.base_type_name)
@@ -5,16 +5,36 @@ require "goon_model_gen/golang/type"
5
5
  module GoonModelGen
6
6
  module Golang
7
7
  class Enum < Type
8
- attr_reader :base_type_name, :map
8
+ class Element
9
+ attr_reader :value, :name
10
+ def initialize(value, name)
11
+ @value, @name = value, name
12
+ end
13
+ end
14
+
15
+ attr_reader :base_type_name, :elements
9
16
  attr_reader :base_type
10
17
 
11
18
  # @param name [String]
12
19
  # @param base_type_name [String]
13
- # @param map [Hash<Object,String>]
14
- def initialize(name, base_type_name, map)
20
+ def initialize(name, base_type_name)
15
21
  super(name)
16
22
  @base_type_name = base_type_name
17
- @map = map
23
+ @elements = []
24
+ end
25
+
26
+ # @param value [Object]
27
+ # @param name [String]
28
+ def add(value, name)
29
+ elements << Element.new(value, name)
30
+ end
31
+
32
+ # @yieldparam value [Object]
33
+ # @yieldparam name [String]
34
+ def each_value_and_name
35
+ elements.each do |i|
36
+ yield(i.value, i.name)
37
+ end
18
38
  end
19
39
 
20
40
  # @param pkgs [Packages]
@@ -57,10 +57,9 @@ module GoonModelGen
57
57
 
58
58
  # @param name [string]
59
59
  # @param base_type [String]
60
- # @param map [Hash<Object,Hash>] elements of enum from YAML
61
60
  # @return [Enum]
62
- def new_enum(name, base_type, map)
63
- Enum.new(name, base_type, map).tap{|s| add(s) }
61
+ def new_enum(name, base_type)
62
+ Enum.new(name, base_type).tap{|s| add(s) }
64
63
  end
65
64
 
66
65
  # @param name [string]
@@ -5,15 +5,28 @@ require "goon_model_gen/source/type"
5
5
  module GoonModelGen
6
6
  module Source
7
7
  class Enum < Type
8
- attr_reader :base_type, :map
8
+
9
+ class Element
10
+ attr_reader :value, :name
11
+ def initialize(value, name)
12
+ @value, @name = value, name
13
+ end
14
+ end
15
+
16
+ attr_reader :base_type, :elements
9
17
 
10
18
  # @param name [String]
11
19
  # @param base_type [String]
12
- # @param map [Hash<Object,String>]
13
- def initialize(name, base_type, map)
20
+ # @param element_definitions [Array<Hash<Object,String>>,Hash<Object,String>]
21
+ def initialize(name, base_type, element_definitions)
22
+ unless element_definitions.all?{|i| i.is_a?(Hash) && (i.length == 1) }
23
+ raise "Enum element definitions must be an Array of 1 element Hash but was #{element_definitions.inspect}"
24
+ end
14
25
  super(name)
15
26
  @base_type = base_type
16
- @map = map
27
+ @elements = element_definitions.map do |i|
28
+ Element.new(i.keys.first, i.values.first)
29
+ end
17
30
  end
18
31
  end
19
32
  end
@@ -35,10 +35,10 @@ module GoonModelGen
35
35
 
36
36
  # @param name [string]
37
37
  # @param base_type [String]
38
- # @param map [Hash<Object,Hash>] elements of enum from YAML
38
+ # @param elements [Hash<Object,Hash>] elements of enum from YAML
39
39
  # @return [Enum]
40
- def new_enum(name, base_type, map)
41
- Enum.new(name, base_type, map).tap do |t|
40
+ def new_enum(name, base_type, elements)
41
+ Enum.new(name, base_type, elements).tap do |t|
42
42
  t.context = self.context
43
43
  types.push(t)
44
44
  end
@@ -42,7 +42,9 @@ module GoonModelGen
42
42
  end
43
43
  end
44
44
  elsif t['enum_map'].is_a?(Hash) && t['base']
45
- f.new_enum(name, t['base'], t['enum_map'])
45
+ f.new_enum(name, t['base'], t['enum_map'].map{|k,v| {k => v}})
46
+ elsif t['enum'].is_a?(Array) && t['base']
47
+ f.new_enum(name, t['base'], t['enum'])
46
48
  elsif t['slice_of'].is_a?(String)
47
49
  f.new_named_slice(name, t['slice_of'])
48
50
  else
@@ -1,7 +1,7 @@
1
1
  type <%= type.name %> <%= type.base_type.qualified_name %>
2
2
 
3
3
  const (
4
- <%- type.map.each do |key, name| -%>
4
+ <%- type.each_value_and_name do |key, name| -%>
5
5
  <%= name.camelize %> <%= type.name %> = <%= key.inspect %>
6
6
  <%- end -%>
7
7
  )
@@ -1,5 +1,5 @@
1
1
  var All<%= type.name %> = []<%= type.name %>{
2
- <%- type.map.each do |key, name| -%>
2
+ <%- type.each_value_and_name do |key, name| -%>
3
3
  <%= name.camelize %>,
4
4
  <%- end -%>
5
5
  }
@@ -1,5 +1,5 @@
1
1
  var <%= type.name %>Map = map[<%= type.name %>]string{
2
- <%- type.map.each do |key, name| -%>
2
+ <%- type.each_value_and_name do |key, name| -%>
3
3
  <%= name.camelize %>: <%= name.inspect %>,
4
4
  <%- end -%>
5
5
  }
@@ -1,5 +1,5 @@
1
1
  var <%= type.name %>NameToValue = map[string]<%= type.name %>{
2
- <%- type.map.each do |key, name| -%>
2
+ <%- type.each_value_and_name do |key, name| -%>
3
3
  <%= name.inspect %>: <%= key.inspect %>,
4
4
  <%- end -%>
5
5
  }
@@ -1,3 +1,3 @@
1
1
  module GoonModelGen
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: goon_model_gen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - akm
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-13 00:00:00.000000000 Z
11
+ date: 2019-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor