xml2go 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/xml2go/parser.rb +6 -4
- data/lib/xml2go/struct.rb +35 -33
- data/lib/xml2go/version.rb +1 -1
- metadata +2 -2
data/lib/xml2go/parser.rb
CHANGED
@@ -44,7 +44,8 @@ module Xml2Go
|
|
44
44
|
|
45
45
|
# take 's' out of string and return if it was plural or not
|
46
46
|
def singularize(string)
|
47
|
-
|
47
|
+
# I need a singulizer util :(
|
48
|
+
if string[-1] == "s" && string[-1] != string[-2] then
|
48
49
|
s = string[0..-2]
|
49
50
|
return [s, true]
|
50
51
|
end
|
@@ -59,9 +60,9 @@ module Xml2Go
|
|
59
60
|
end
|
60
61
|
|
61
62
|
def add_field_to_struct(struct, var_name, type, xml_tag)
|
62
|
-
if struct.fields.has_key?(var_name) &&
|
63
|
+
if struct.fields.has_key?(var_name) &&
|
63
64
|
!struct.fields[var_name].type.include?("[]") then
|
64
|
-
|
65
|
+
|
65
66
|
type, sing = singularize(struct.fields[var_name].type)
|
66
67
|
|
67
68
|
struct.fields[var_name].type = "[]" + type
|
@@ -121,6 +122,7 @@ module Xml2Go
|
|
121
122
|
|
122
123
|
def parse_element(element)
|
123
124
|
struct_name = normalize(element.name)
|
125
|
+
struct_name, s = singularize(struct_name)
|
124
126
|
# TODO: maybe we DO want to process repeated structs
|
125
127
|
# to capture arrays don't process to structs
|
126
128
|
return if @structs.has_key?(struct_name)
|
@@ -143,7 +145,7 @@ module Xml2Go
|
|
143
145
|
end
|
144
146
|
end
|
145
147
|
|
146
|
-
@structs[struct.
|
148
|
+
@structs[struct.type] = struct
|
147
149
|
end
|
148
150
|
|
149
151
|
# analyses the xml element to try and fetch the type
|
data/lib/xml2go/struct.rb
CHANGED
@@ -1,42 +1,42 @@
|
|
1
1
|
|
2
2
|
module Xml2Go
|
3
3
|
|
4
|
-
#
|
5
|
-
class
|
6
|
-
attr_accessor :
|
7
|
-
|
8
|
-
# represents member variables
|
9
|
-
class Field
|
10
|
-
attr_accessor :type, :name, :xml_tag, :value
|
11
|
-
|
12
|
-
def initialize(name, type, xml_tag, value)
|
13
|
-
@name = name
|
14
|
-
@type = type
|
15
|
-
@xml_tag = xml_tag
|
16
|
-
@value = value
|
17
|
-
@const_name = Xml2Go::get_const_name(@name)
|
18
|
-
end
|
4
|
+
# represents member variables
|
5
|
+
class Field
|
6
|
+
attr_accessor :type, :name, :xml_tag, :value
|
19
7
|
|
20
|
-
|
21
|
-
|
22
|
-
|
8
|
+
def initialize(name, type, xml_tag, value)
|
9
|
+
@name = name
|
10
|
+
@type = type
|
11
|
+
@xml_tag = xml_tag
|
12
|
+
@value = value
|
13
|
+
@const_name = Xml2Go::get_const_name(@name)
|
14
|
+
end
|
23
15
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
16
|
+
def to_s
|
17
|
+
"#{@name} #{@type} `xml:\"#{@xml_tag}\"`"
|
18
|
+
end
|
28
19
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
return ""
|
34
|
-
end
|
20
|
+
def to_declaration
|
21
|
+
value = @value.nil? ? Xml2Go::low(@name) : @const_name
|
22
|
+
"#{@name}: #{value},"
|
23
|
+
end
|
35
24
|
|
25
|
+
def to_const
|
26
|
+
if !value.nil? then
|
27
|
+
return "#{@const_name} = \"#{@value}\""
|
28
|
+
end
|
29
|
+
return ""
|
36
30
|
end
|
37
31
|
|
38
|
-
|
39
|
-
|
32
|
+
end # class Field
|
33
|
+
|
34
|
+
# class representing a Go struct
|
35
|
+
class Struct
|
36
|
+
attr_accessor :fields, :type
|
37
|
+
|
38
|
+
def initialize(type)
|
39
|
+
@type = type
|
40
40
|
@fields = {}
|
41
41
|
end
|
42
42
|
|
@@ -52,7 +52,7 @@ module Xml2Go
|
|
52
52
|
|
53
53
|
def to_s
|
54
54
|
fields_string = @fields.values.join("\n")
|
55
|
-
"type #{@
|
55
|
+
"type #{@type} struct {\n
|
56
56
|
#{fields_string}
|
57
57
|
}
|
58
58
|
"
|
@@ -60,15 +60,17 @@ module Xml2Go
|
|
60
60
|
|
61
61
|
def to_declaration
|
62
62
|
fields_string = @fields.values.map { |v| v.to_declaration}
|
63
|
-
"#{Xml2Go::low(@
|
63
|
+
"#{Xml2Go::low(@type)} := #{@type} {
|
64
64
|
#{fields_string.join("\n")}
|
65
65
|
}"
|
66
66
|
end
|
67
67
|
|
68
68
|
def get_consts
|
69
69
|
consts_string = @fields.values.map{ |v| v.to_const}
|
70
|
-
"// #{@
|
70
|
+
"// #{@type} info \n" << consts_string.join("\n")
|
71
71
|
end
|
72
72
|
|
73
73
|
end
|
74
|
+
|
75
|
+
|
74
76
|
end
|
data/lib/xml2go/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xml2go
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-07-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|