solara 0.7.3 → 0.7.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/solara/lib/core/scripts/code_generator/dart_code_generator.rb +47 -13
- data/solara/lib/core/scripts/code_generator/kotlin_code_generator.rb +57 -19
- data/solara/lib/core/scripts/code_generator/swift_code_generator.rb +69 -33
- data/solara/lib/solara/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77f9761ea65efa893a892df04d7d5fad6a24c9a5437dc4e2ace86b741feb5df3
|
4
|
+
data.tar.gz: edb89227453aa0d01ee0f43129c8196dbcd74dffa8abfe69f1bd2053ce63e92f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 561614f023227070c3a24577d8bad4b8a6bb37024a251e3145a1702df798bc1e203fb6772fcd5bf7eee8ffe1bcb91dfe15ac0bef21b29dadab4065cd09a2d595
|
7
|
+
data.tar.gz: b1be57455047e46a966211cbd612cd16179a3d5789affca52dc0e8f68308b7138d4a6ae6cc8c3e488b34453f9c2ee681da57fa7f62ab20343a4e55353a8e1ac4
|
@@ -14,7 +14,7 @@ class DartCodeGenerator
|
|
14
14
|
|
15
15
|
private
|
16
16
|
|
17
|
-
def generate_class(json_obj, class_name)
|
17
|
+
def generate_class(json_obj, class_name, list_items = nil)
|
18
18
|
@registry.register(class_name, class_name)
|
19
19
|
class_name = @registry.get_class_name(class_name, class_name)
|
20
20
|
return if @generated_classes.any? { |c| c.include?("class #{class_name}") }
|
@@ -34,36 +34,70 @@ class DartCodeGenerator
|
|
34
34
|
if value.is_a?(Hash)
|
35
35
|
generate_class(value, nested_class_name)
|
36
36
|
companion_values << "static final #{key}Value = #{nested_class_name}.instance;"
|
37
|
+
instance_values << "#{key}: #{nested_class_name}.instance"
|
37
38
|
elsif value.is_a?(Array) && !value.empty? && value.first.is_a?(Hash)
|
38
|
-
generate_class(value.first, nested_class_name)
|
39
|
+
generate_class(value.first, nested_class_name, value)
|
39
40
|
type = "List<#{nested_class_name}>"
|
40
|
-
|
41
|
-
|
41
|
+
companion_values << "static final #{key}Value = #{nested_class_name}.instances;"
|
42
|
+
instance_values << "#{key}: #{nested_class_name}.instances"
|
42
43
|
else
|
43
44
|
companion_values << "static final #{key}Value = #{dart_value(value)};"
|
45
|
+
instance_values << "#{key}: #{key}Value"
|
44
46
|
end
|
45
47
|
|
46
48
|
properties << "final #{type} #{key};"
|
47
49
|
constructor_params << "required this.#{key}"
|
48
|
-
instance_values << "#{key}: #{key}Value"
|
49
50
|
end
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
52
|
+
static_instances = if list_items
|
53
|
+
items_code = list_items.map.with_index do |item, index|
|
54
|
+
values = item.map do |key, value|
|
55
|
+
if value.is_a?(Hash)
|
56
|
+
nested_class_name = "#{class_name}#{StringCase.capitalize(key)}"
|
57
|
+
@registry.register(nested_class_name, nested_class_name)
|
58
|
+
nested_class_name = @registry.get_class_name(nested_class_name, nested_class_name)
|
59
|
+
"#{key}: #{nested_class_name}.instance"
|
60
|
+
elsif value.is_a?(Array) && !value.empty? && value.first.is_a?(Hash)
|
61
|
+
nested_class_name = "#{class_name}#{StringCase.capitalize(key)}"
|
62
|
+
@registry.register(nested_class_name, nested_class_name)
|
63
|
+
nested_class_name = @registry.get_class_name(nested_class_name, nested_class_name)
|
64
|
+
"#{key}: #{nested_class_name}.instances"
|
65
|
+
else
|
66
|
+
"#{key}: #{dart_value(value)}"
|
67
|
+
end
|
68
|
+
end.join(",\n ")
|
69
|
+
" static final instance#{index + 1} = #{class_name}(\n #{values}\n );"
|
70
|
+
end.join("\n")
|
71
|
+
|
72
|
+
instances_list = (1..list_items.length).map { |i| "instance#{i}" }.join(", ")
|
73
|
+
|
74
|
+
<<~DART
|
75
|
+
#{items_code}
|
76
|
+
|
77
|
+
static final instances = <#{class_name}>[#{instances_list}];
|
78
|
+
DART
|
79
|
+
else
|
80
|
+
<<~DART
|
57
81
|
static #{class_name}? _instance;
|
58
82
|
|
59
|
-
#{companion_values.join("\n ")}
|
60
|
-
|
61
83
|
static #{class_name} get instance {
|
62
84
|
_instance ??= #{class_name}(
|
63
85
|
#{instance_values.join(",\n ")}
|
64
86
|
);
|
65
87
|
return _instance!;
|
66
88
|
}
|
89
|
+
DART
|
90
|
+
end
|
91
|
+
|
92
|
+
class_code = <<~DART
|
93
|
+
class #{class_name} {
|
94
|
+
#{properties.join("\n ")}
|
95
|
+
|
96
|
+
const #{class_name}({#{constructor_params.join(", ")}});
|
97
|
+
|
98
|
+
#{companion_values.join("\n ")}
|
99
|
+
|
100
|
+
#{static_instances}
|
67
101
|
}
|
68
102
|
DART
|
69
103
|
|
@@ -14,13 +14,14 @@ class KotlinCodeGenerator
|
|
14
14
|
|
15
15
|
private
|
16
16
|
|
17
|
-
def generate_class(json_obj, class_name)
|
17
|
+
def generate_class(json_obj, class_name, list_items = nil)
|
18
18
|
@registry.register(class_name, class_name)
|
19
19
|
class_name = @registry.get_class_name(class_name, class_name)
|
20
20
|
return if @generated_classes.any? { |c| c.include?("data class #{class_name}") }
|
21
21
|
|
22
22
|
properties = []
|
23
23
|
companion_values = []
|
24
|
+
instance_values = []
|
24
25
|
|
25
26
|
json_obj.each do |key, value|
|
26
27
|
nested_class_name = "#{class_name}#{StringCase.capitalize(key)}"
|
@@ -32,37 +33,74 @@ class KotlinCodeGenerator
|
|
32
33
|
if value.is_a?(Hash)
|
33
34
|
generate_class(value, nested_class_name)
|
34
35
|
companion_values << "private val #{key}Value = #{nested_class_name}.instance"
|
36
|
+
instance_values << "#{key} = #{nested_class_name}.instance"
|
35
37
|
elsif value.is_a?(Array) && !value.empty? && value.first.is_a?(Hash)
|
36
|
-
generate_class(value.first, nested_class_name)
|
38
|
+
generate_class(value.first, nested_class_name, value)
|
37
39
|
type = "List<#{nested_class_name}>"
|
38
|
-
|
39
|
-
|
40
|
+
companion_values << "private val #{key}Value = #{nested_class_name}.instances"
|
41
|
+
instance_values << "#{key} = #{nested_class_name}.instances"
|
40
42
|
else
|
41
43
|
companion_values << "private val #{key}Value = #{kotlin_value(value)}"
|
44
|
+
instance_values << "#{key} = #{key}Value"
|
42
45
|
end
|
43
46
|
|
44
47
|
properties << "val #{key}: #{type}"
|
45
48
|
end
|
46
49
|
|
50
|
+
static_instances = if list_items
|
51
|
+
items_code = list_items.map.with_index do |item, index|
|
52
|
+
values = item.map do |key, value|
|
53
|
+
if value.is_a?(Hash)
|
54
|
+
nested_class_name = "#{class_name}#{StringCase.capitalize(key)}"
|
55
|
+
@registry.register(nested_class_name, nested_class_name)
|
56
|
+
nested_class_name = @registry.get_class_name(nested_class_name, nested_class_name)
|
57
|
+
"#{key} = #{nested_class_name}.instance"
|
58
|
+
elsif value.is_a?(Array) && !value.empty? && value.first.is_a?(Hash)
|
59
|
+
nested_class_name = "#{class_name}#{StringCase.capitalize(key)}"
|
60
|
+
@registry.register(nested_class_name, nested_class_name)
|
61
|
+
nested_class_name = @registry.get_class_name(nested_class_name, nested_class_name)
|
62
|
+
"#{key} = #{nested_class_name}.instances"
|
63
|
+
else
|
64
|
+
"#{key} = #{kotlin_value(value)}"
|
65
|
+
end
|
66
|
+
end.join(",\n ")
|
67
|
+
" private val instance#{index + 1} = #{class_name}(\n #{values}\n )"
|
68
|
+
end.join("\n")
|
69
|
+
|
70
|
+
instances_list = (1..list_items.length).map { |i| "instance#{i}" }.join(", ")
|
71
|
+
|
72
|
+
<<~KOTLIN
|
73
|
+
companion object {
|
74
|
+
#{items_code}
|
75
|
+
|
76
|
+
val instances = listOf(#{instances_list})
|
77
|
+
}
|
78
|
+
KOTLIN
|
79
|
+
else
|
80
|
+
<<~KOTLIN
|
81
|
+
companion object {
|
82
|
+
private var _instance: #{class_name}? = null
|
83
|
+
|
84
|
+
#{companion_values.join("\n ")}
|
85
|
+
|
86
|
+
val instance: #{class_name}
|
87
|
+
get() {
|
88
|
+
if (_instance == null) {
|
89
|
+
_instance = #{class_name}(
|
90
|
+
#{instance_values.join(",\n ")}
|
91
|
+
)
|
92
|
+
}
|
93
|
+
return _instance!!
|
94
|
+
}
|
95
|
+
}
|
96
|
+
KOTLIN
|
97
|
+
end
|
98
|
+
|
47
99
|
class_code = <<~KOTLIN
|
48
100
|
data class #{class_name}(
|
49
101
|
#{properties.join(",\n ")}
|
50
102
|
) {
|
51
|
-
|
52
|
-
private var _instance: #{class_name}? = null
|
53
|
-
|
54
|
-
#{companion_values.join("\n ")}
|
55
|
-
|
56
|
-
val instance: #{class_name}
|
57
|
-
get() {
|
58
|
-
if (_instance == null) {
|
59
|
-
_instance = #{class_name}(
|
60
|
-
#{json_obj.keys.map { |key| "#{key} = #{key}Value" }.join(",\n ")}
|
61
|
-
)
|
62
|
-
}
|
63
|
-
return _instance!!
|
64
|
-
}
|
65
|
-
}
|
103
|
+
#{static_instances}
|
66
104
|
}
|
67
105
|
KOTLIN
|
68
106
|
|
@@ -1,26 +1,27 @@
|
|
1
1
|
class SwiftCodeGenerator
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
def generate_class(json_obj, class_name)
|
2
|
+
def initialize(json, parent_class_name, registry = nil)
|
3
|
+
@json = json
|
4
|
+
@parent_class_name = parent_class_name
|
5
|
+
@registry = registry || ClassNameRegistry.new
|
6
|
+
@generated_classes = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def generate
|
10
|
+
imports = "import UIKit\n\n"
|
11
|
+
generate_class(@json, @parent_class_name)
|
12
|
+
imports + @generated_classes.reverse.join("\n\n")
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def generate_class(json_obj, class_name, list_items = nil)
|
18
18
|
@registry.register(class_name, class_name)
|
19
19
|
class_name = @registry.get_class_name(class_name, class_name)
|
20
20
|
return if @generated_classes.any? { |c| c.include?("struct #{class_name}") }
|
21
21
|
|
22
22
|
properties = []
|
23
23
|
static_values = []
|
24
|
+
instance_values = []
|
24
25
|
|
25
26
|
json_obj.each do |key, value|
|
26
27
|
nested_class_name = "#{class_name}#{StringCase.capitalize(key)}"
|
@@ -32,34 +33,69 @@ class SwiftCodeGenerator
|
|
32
33
|
if value.is_a?(Hash)
|
33
34
|
generate_class(value, nested_class_name)
|
34
35
|
static_values << "private static let #{key}Value = #{nested_class_name}.shared"
|
36
|
+
instance_values << "#{key}: #{nested_class_name}.shared"
|
35
37
|
elsif value.is_a?(Array) && !value.empty? && value.first.is_a?(Hash)
|
36
|
-
generate_class(value.first, nested_class_name)
|
38
|
+
generate_class(value.first, nested_class_name, value)
|
37
39
|
type = "[#{nested_class_name}]"
|
38
|
-
|
39
|
-
|
40
|
+
static_values << "private static let #{key}Value = #{nested_class_name}.instances"
|
41
|
+
instance_values << "#{key}: #{nested_class_name}.instances"
|
40
42
|
else
|
41
43
|
static_values << "private static let #{key}Value = #{swift_value(value)}"
|
44
|
+
instance_values << "#{key}: #{key}Value"
|
42
45
|
end
|
43
46
|
|
44
47
|
properties << "let #{key}: #{type}"
|
45
48
|
end
|
46
49
|
|
50
|
+
static_instances = if list_items
|
51
|
+
items_code = list_items.map.with_index do |item, index|
|
52
|
+
values = item.map do |key, value|
|
53
|
+
if value.is_a?(Hash)
|
54
|
+
nested_class_name = "#{class_name}#{StringCase.capitalize(key)}"
|
55
|
+
@registry.register(nested_class_name, nested_class_name)
|
56
|
+
nested_class_name = @registry.get_class_name(nested_class_name, nested_class_name)
|
57
|
+
"#{key}: #{nested_class_name}.shared"
|
58
|
+
elsif value.is_a?(Array) && !value.empty? && value.first.is_a?(Hash)
|
59
|
+
nested_class_name = "#{class_name}#{StringCase.capitalize(key)}"
|
60
|
+
@registry.register(nested_class_name, nested_class_name)
|
61
|
+
nested_class_name = @registry.get_class_name(nested_class_name, nested_class_name)
|
62
|
+
"#{key}: #{nested_class_name}.instances"
|
63
|
+
else
|
64
|
+
"#{key}: #{swift_value(value)}"
|
65
|
+
end
|
66
|
+
end.join(",\n ")
|
67
|
+
" private static let instance#{index + 1} = #{class_name}(\n #{values}\n )"
|
68
|
+
end.join("\n")
|
69
|
+
|
70
|
+
instances_list = (1..list_items.length).map { |i| "instance#{i}" }.join(", ")
|
71
|
+
|
72
|
+
<<~SWIFT
|
73
|
+
#{items_code}
|
74
|
+
|
75
|
+
static let instances: [#{class_name}] = [#{instances_list}]
|
76
|
+
SWIFT
|
77
|
+
else
|
78
|
+
<<~SWIFT
|
79
|
+
private static var _shared: #{class_name}?
|
80
|
+
|
81
|
+
#{static_values.join("\n ")}
|
82
|
+
|
83
|
+
static var shared: #{class_name} {
|
84
|
+
if _shared == nil {
|
85
|
+
_shared = #{class_name}(
|
86
|
+
#{instance_values.join(",\n ")}
|
87
|
+
)
|
88
|
+
}
|
89
|
+
return _shared!
|
90
|
+
}
|
91
|
+
SWIFT
|
92
|
+
end
|
93
|
+
|
47
94
|
class_code = <<~SWIFT
|
48
95
|
struct #{class_name} {
|
49
|
-
#{properties.join("\n
|
50
|
-
|
51
|
-
private static var _shared: #{class_name}?
|
52
|
-
|
53
|
-
#{static_values.join("\n ")}
|
96
|
+
#{properties.join("\n ")}
|
54
97
|
|
55
|
-
|
56
|
-
if _shared == nil {
|
57
|
-
_shared = #{class_name}(
|
58
|
-
#{json_obj.keys.map { |key| "#{key}: #{key}Value" }.join(",\n ")}
|
59
|
-
)
|
60
|
-
}
|
61
|
-
return _shared!
|
62
|
-
}
|
98
|
+
#{static_instances}
|
63
99
|
}
|
64
100
|
SWIFT
|
65
101
|
|
@@ -105,4 +141,4 @@ class SwiftCodeGenerator
|
|
105
141
|
return "nil" if value.nil?
|
106
142
|
value.to_s
|
107
143
|
end
|
108
|
-
end
|
144
|
+
end
|