jaspion-kilza 1.0.9 → 1.1.0

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
  SHA1:
3
- metadata.gz: eb863f539668936bbfcc38b09be56efe00fc9c58
4
- data.tar.gz: 4cdea48ae8354c0b4d1dcb48238a0c21848c51c7
3
+ metadata.gz: a005f85f95e01c815452123d02f78882c2e6963e
4
+ data.tar.gz: d948698396da25947d4df8ce25d5f3a19fd90765
5
5
  SHA512:
6
- metadata.gz: 54acf2e90227058d1a6dc5c31d683bf42834f5a02ffe77bc695de8939d840be17b0426da5d476db5b86bdc73d9963f89a062af668a98583d2076ad48ca7f013b
7
- data.tar.gz: 2895c608667a76fff1a22820532aad55faa9265154e953d68c9bdaa2dac36c478c6f7a2adc74eaecbcdb32bdc7e5b7b25522660fe3a64e47fa56ce7f441a0632
6
+ metadata.gz: 62e6310c809fc212626296a14e2dacc8b58e185e2049e2777642bfa4abe50faf5ff726fbf8089ce6dde4e9ae3b5019f09f1d662e3b715bc1e61d4f604f00dcbd
7
+ data.tar.gz: 237145797b973ce882e5b7fa2699133c266ecb57f0ac3f878cf3beceb9862dc4b28b8f21e327fda0f46f64019184403c551fdc71217ccf4b4576e749c2ea14d4
data/bin/kilza CHANGED
@@ -5,7 +5,6 @@ require 'jaspion/kilza'
5
5
  require 'pastel'
6
6
  require 'tty'
7
7
  require 'tty-prompt'
8
- require 'tty-spinner'
9
8
  require 'net/http'
10
9
  require 'uri'
11
10
 
@@ -27,11 +26,11 @@ class KilzaApplication
27
26
 
28
27
  OBJC = 1
29
28
  JAVA = 2
29
+ SWIFT = 3
30
30
 
31
31
  def initialize
32
32
  @pastel = Pastel.new
33
33
  @prompt = TTY::Prompt.new
34
- @spinner = TTY::Spinner.new
35
34
 
36
35
  @done = false
37
36
  end
@@ -42,6 +41,7 @@ class KilzaApplication
42
41
  menu.enum '.'
43
42
 
44
43
  menu.choice 'Objective-C', OBJC
44
+ menu.choice 'Swift', SWIFT
45
45
  menu.choice 'Java', JAVA
46
46
  end
47
47
  action
@@ -88,6 +88,13 @@ class KilzaApplication
88
88
  File.write(File.join(target_path, s.file_name), s.source)
89
89
  }
90
90
  }
91
+ elsif (lang == SWIFT)
92
+ swift = Jaspion::Kilza::Swift.new(json_string)
93
+ swift.classes(class_basename).each { |c|
94
+ c.sources.each{ |s|
95
+ File.write(File.join(target_path, s.file_name), s.source)
96
+ }
97
+ }
91
98
  end
92
99
  end
93
100
 
@@ -125,8 +132,8 @@ class KilzaApplication
125
132
  @prompt.say("\n")
126
133
  @prompt.say(@pastel.bold('Target: ') + target_path)
127
134
  @prompt.say(@pastel.bold('Base class name: ') + class_basename)
128
- @prompt.say(@pastel.bold('Target language: ') + (lang == OBJC ? 'Objective-C' : 'Java'))
129
- if not class_package.nil?
135
+ @prompt.say(@pastel.bold('Target language: ') + (lang == OBJC ? 'Objective-C' : (lang == SWIFT ? 'Swift' : 'Java')))
136
+ unless class_package.nil?
130
137
  @prompt.say(@pastel.bold('Target package: ') + class_package)
131
138
  end
132
139
  ok = @prompt.yes?(@pastel.bold('Is that correct? '))
@@ -6,6 +6,7 @@ require 'jaspion/kilza/property'
6
6
  require 'jaspion/kilza/language'
7
7
  require 'jaspion/kilza/language/objc'
8
8
  require 'jaspion/kilza/language/java'
9
+ require 'jaspion/kilza/language/swift'
9
10
 
10
11
  # Ruby class
11
12
  class String
@@ -47,6 +47,7 @@ module Jaspion
47
47
  #
48
48
  # @return [Array] All available classes
49
49
  def classes(base_name)
50
+ @classes = []
50
51
  hash = JSON.parse(json_string)
51
52
  hash = { base_name + 'Object' => hash } if hash.is_a?(Array)
52
53
  parse_hash(base_name, hash)
@@ -22,6 +22,70 @@ module Jaspion
22
22
  end
23
23
  end
24
24
 
25
+ module Jaspion
26
+ module Kilza
27
+ class Java
28
+ class Property < Jaspion::Kilza::Property
29
+
30
+ def json_key
31
+ return ' private static final String FIELD_' +
32
+ @name.upcase + ' = "' + @original_name + '";'
33
+ end
34
+
35
+ def declaration
36
+ r = %( @Expose
37
+ @SerializedName(FIELD_#{@name.upcase})
38
+ )
39
+
40
+ if array?
41
+ r + " private ArrayList<#{@type}> #{@name};"
42
+ else
43
+ r + " private #{@type} #{@name};"
44
+ end
45
+ end
46
+
47
+ def parse_element
48
+ if object?
49
+ " this.#{@name} = new #{@type}(object.optJSONObject(FIELD_#{@name.upcase}));"
50
+ elsif null?
51
+ " this.#{@name} = object.opt(FIELD_#{@name.upcase});"
52
+ else
53
+ " this.#{@name} = object.opt#{@type}(FIELD_#{@name.upcase});"
54
+ end
55
+ end
56
+
57
+ def parse_array
58
+ r = %( if (object.optJSONArray(FIELD_#{@name.upcase}) != null)
59
+ {
60
+ this.#{@name} = new ArrayList<>();
61
+ JSONArray #{@name}JsonArray = object.optJSONArray(FIELD_#{@name.upcase});
62
+ for (int i = 0; i < #{@name}JsonArray.length(); i++) {
63
+ )
64
+
65
+ if object? || null?
66
+ r = r + " JSONObject #{@name} = #{@name}JsonArray.optJSONObject(i);"
67
+ else
68
+ r = r + " #{@type} #{@name} = #{@name}JsonArray.optJSON#{@type}(i);"
69
+ end
70
+ r = r + %(
71
+ this.#{@name}.add(new #{@type}(#{@name}));
72
+ }
73
+ })
74
+ return r
75
+ end
76
+
77
+ def parse_json
78
+ if array?
79
+ parse_array
80
+ else
81
+ parse_element
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+
25
89
  module Jaspion
26
90
  module Kilza
27
91
  # Objective-C Language parser
@@ -56,6 +120,14 @@ module Jaspion
56
120
  Jaspion::Kilza::Java::Class.new(name)
57
121
  end
58
122
 
123
+ def property(name, type, array, key)
124
+ original_name = name
125
+ name = @reserved_delimiter + name unless @reserved_words.index(name).nil?
126
+ prop = Jaspion::Kilza::Java::Property.new(name , type, array, key)
127
+ prop.original_name = original_name
128
+ prop
129
+ end
130
+
59
131
  def classes(class_name)
60
132
  super(class_name)
61
133
 
@@ -14,17 +14,11 @@ import com.google.gson.annotations.Expose;
14
14
  public class <%= @name %> implements Serializable
15
15
  {
16
16
  <% for @property in @properties %>
17
- private static final String FIELD_<%= @property.name.upcase %> = "<%= @property.original_name %>";
17
+ <%= @property.json_key %>
18
18
  <% end %>
19
19
 
20
20
  <% for @property in @properties %>
21
- @Expose
22
- @SerializedName(FIELD_<%= @property.name.upcase %>)
23
- <% if @property.array? %>
24
- private ArrayList<<%= @property.type %>> <%= @property.name %>;
25
- <% else %>
26
- private <%= @property.type %> <%= @property.name %>;
27
- <% end %>
21
+ <%= @property.declaration %>
28
22
  <% end %>
29
23
 
30
24
  public <%= @name %>() {
@@ -55,31 +49,7 @@ public class <%= @name %> implements Serializable
55
49
  protected void parseObject(JSONObject object)
56
50
  {
57
51
  <% for @property in @properties %>
58
- <% if @property.array? %>
59
- if (object.optJSONArray(FIELD_<%= @property.name.upcase %>) != null)
60
- {
61
- this.<%= @property.name %> = new ArrayList<>();
62
- JSONArray <%= @property.name %>JsonArray = object.optJSONArray(FIELD_<%= @property.name.upcase %>);
63
- for (int i = 0; i < <%= @property.name %>JsonArray.length(); i++) {
64
- <% if @property.object? || @property.null? %>
65
- JSONObject <%= @property.name %> = <%= @property.name %>JsonArray.optJSONObject(i);
66
- <% else %>
67
- <%= @property.type %> <%= @property.name %> = <%= @property.name %>JsonArray.optJSON<%= @property.type %>(i);
68
- <% end %>
69
- this.<%= @property.name %>.add(new <%= @property.type %>(<%= @property.name %>));
70
- }
71
- }
72
- <% else %>
73
- <% if @property.object? %>
74
- this.<%= @property.name %> = new <%= @property.type %>(object.optJSONObject(FIELD_<%= @property.name.upcase %>));
75
- <% else %>
76
- <% if @property.null? %>
77
- this.<%= @property.name %> = object.opt(FIELD_<%= @property.name.upcase %>);
78
- <% else %>
79
- this.<%= @property.name %> = object.opt<%= @property.type %>(FIELD_<%= @property.name.upcase %>);
80
- <% end %>
81
- <% end %>
82
- <% end %>
52
+ <%= @property.parse_json %>
83
53
  <% end %>
84
54
  }
85
55
 
@@ -3,6 +3,7 @@
3
3
  //
4
4
  // Created on <%= Time.now.strftime("%Y-%m-%d") %>
5
5
  // Copyright (c) <%= Time.now.strftime("%Y") %>. All rights reserved.
6
+ // Generated by Kilza https://github.com/Jaspion/Kilza
6
7
  //
7
8
 
8
9
  #import <Foundation/Foundation.h>
@@ -3,6 +3,7 @@
3
3
  //
4
4
  // Created on <%= Time.now.strftime("%Y-%m-%d") %>
5
5
  // Copyright (c) <%= Time.now.strftime("%Y") %>. All rights reserved.
6
+ // Generated by Kilza https://github.com/Jaspion/Kilza
6
7
  //
7
8
 
8
9
  #import "<%= @name %>.h"
@@ -0,0 +1,74 @@
1
+ require 'date'
2
+
3
+ module Jaspion
4
+ module Kilza
5
+ class Swift
6
+ class Class
7
+ include Jaspion::Kilza::Class
8
+
9
+ def sources
10
+ [code('swift', 'swift')]
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ module Jaspion
18
+ module Kilza
19
+ # Swift Language parser
20
+ class Swift
21
+ include Jaspion::Kilza::Language
22
+
23
+ def initialize(json_string)
24
+ super(json_string)
25
+
26
+ @reserved_delimiter = '_my'
27
+
28
+ @reserved_words = %w(
29
+ class break as associativity deinit case dynamicType
30
+ convenience enum continue false dynamic extension default
31
+ is didSet func do nil final import else self get init
32
+ fallthrough Self infix internal for super inout let
33
+ if true lazy operator in left private return mutating
34
+ protocol switch none public where nonmutating static
35
+ while optional struct override subscript postfix
36
+ typealias precedence var prefix required right set
37
+ type unowned weak id
38
+ )
39
+
40
+ @types = {
41
+ 'nilclass' => 'AnyObject',
42
+ 'string' => 'String',
43
+ 'fixnum' => 'Int',
44
+ 'float' => 'Double',
45
+ 'falseclass' => 'Bool',
46
+ 'trueclass' => 'Bool',
47
+ 'hash' => 'Dictionary'
48
+ }
49
+
50
+ @equal_keys = 'id identifier uid'
51
+ end
52
+
53
+ def clazz(name)
54
+ Jaspion::Kilza::Swift::Class.new(name)
55
+ end
56
+
57
+ def classes(class_name)
58
+ super(class_name)
59
+
60
+ @classes.each do |cl|
61
+ cl.properties.each do |pr|
62
+ if pr.object? || (pr.array? && pr.null?)
63
+ pr.type = pr.name.capitalize
64
+ cl.imports.push("import #{pr.name.capitalize}")
65
+ end
66
+
67
+ pr.type = @types[pr.type] unless @types[pr.type].nil?
68
+ pr.type = "[#{pr.type}]" if pr.array?
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,134 @@
1
+ //
2
+ // <%= @name %>.m
3
+ //
4
+ // Created on <%= Time.now.strftime("%Y-%m-%d") %>
5
+ // Copyright (c) <%= Time.now.strftime("%Y") %>. All rights reserved.
6
+ // Generated by Kilza https://github.com/Jaspion/Kilza
7
+ //
8
+
9
+ import Foundation
10
+
11
+ public class <%= @name %>: NSObject, NSCoding {
12
+ // Original names
13
+ <% for @property in @properties %>
14
+ static let k<%= @name %><%= @property.name.capitalize %>: String = "<%= @property.original_name.gsub('"', '\"') %>"
15
+ <% end %>
16
+
17
+ <% for @property in @properties %>
18
+ public var <%= @property.name %>: <%= @property.type %>?
19
+ <% end %>
20
+
21
+ public class func model(obj: AnyObject) -> <%= @name %> {
22
+ var instance: <%= @name %>?
23
+ if (obj is String) {
24
+ instance = <%= @name %>.init(str: obj as! String)
25
+ } else if (obj is Dictionary<String, AnyObject>) {
26
+ instance = <%= @name %>.init(dict: obj as! Dictionary)
27
+ }
28
+ return instance!
29
+ }
30
+
31
+ public convenience init(str: String) {
32
+ <% if @properties.length == 1 %>
33
+ var nStr: String = str
34
+ if let trimmed: String = str.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) {
35
+ if !trimmed.hasPrefix("{") {
36
+ nStr = "{ \"\(<%= @name %>.k<%= @name %><%= @properties.first.name.capitalize %>)\" : \(str) }"
37
+ }
38
+ }
39
+
40
+ if let data = nStr.dataUsingEncoding(NSUTF8StringEncoding) {
41
+ <% else %>
42
+ if let data = str.dataUsingEncoding(NSUTF8StringEncoding) {
43
+ <% end %>
44
+ do {
45
+ let object: AnyObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
46
+ self.init(dict: object as! Dictionary)
47
+ } catch _ as NSError {
48
+ self.init(dict: Dictionary())
49
+ }
50
+ } else {
51
+ self.init(dict: Dictionary())
52
+ }
53
+ }
54
+
55
+ public init(dict: Dictionary<String, AnyObject>) {
56
+ super.init()
57
+ <% for @property in @properties %>
58
+ <% if @property.object? || @property.null? %>
59
+ <% if @property.array? %>
60
+ let obj<%= @property.name.capitalize %>: [AnyObject] = dict[<%= @name %>.k<%= @name %><%= @property.name.capitalize %>]! as! [AnyObject]
61
+ var list<%= @property.name.capitalize %> = <%= @property.type %>()
62
+ for item in obj<%= @property.name.capitalize %> {
63
+ if item is Dictionary<String, AnyObject> {
64
+ list<%= @property.name.capitalize %>.append(<%= @property.name.capitalize %>.model(item))
65
+ }
66
+ }
67
+ self.<%= @property.name %> = list<%= @property.name.capitalize %>
68
+ <% else %>
69
+ self.<%= @property.name %> = <%= @property.name.capitalize %>.model(dict[<%= @name %>.k<%= @name %><%= @property.name.capitalize %>]!)
70
+ <% end %>
71
+ <% else %>
72
+ self.<%= @property.name %> = objectOrNil(forKey: <%= @name %>.k<%= @name %><%= @property.name.capitalize %>, fromDictionary:dict)<% if @property.type != 'AnyObject' %> as? <%= @property.type %><% end %>
73
+ <% end %>
74
+ <% end %>
75
+ }
76
+
77
+ public func dictionaryRepresentation() -> Dictionary<String, AnyObject> {
78
+ <% if @properties.length == 0 %>
79
+ let mutableDict: Dictionary = [String: AnyObject]()
80
+ <% else %>
81
+ var mutableDict: Dictionary = [String: AnyObject]()
82
+ <% end %>
83
+ <% for @property in @properties %>
84
+ <% if @property.object? || (@property.null? && @property.array?) %>
85
+ <% if @property.array? %>
86
+ var tempArray<%= @property.name.capitalize %> = [Dictionary<String, AnyObject>]()
87
+ for subArray in self.<%= @property.name %>! {
88
+ if let dicRepresentation: Dictionary<String, AnyObject> = subArray.dictionaryRepresentation() {
89
+ tempArray<%= @property.name.capitalize %>.append(dicRepresentation)
90
+ }
91
+ }
92
+ mutableDict[<%= @name %>.k<%= @name %><%= @property.name.capitalize %>] = Array.init(tempArray<%= @property.name.capitalize %>)
93
+ <% else %>
94
+ if let dic = <%= @property.name %>?.dictionaryRepresentation() {
95
+ mutableDict[<%= @name %>.k<%= @name %><%= @property.name.capitalize %>] = dic
96
+ } else {
97
+ mutableDict[<%= @name %>.k<%= @name %><%= @property.name.capitalize %>] = self.<%= @property.name %>
98
+ }
99
+ <% end %>
100
+ <% else %>
101
+ mutableDict[<%= @name %>.k<%= @name %><%= @property.name.capitalize %>] = self.<%= @property.name %>
102
+ <% end %>
103
+ <% end %>
104
+ return NSDictionary.init(dictionary: mutableDict) as! Dictionary<String, AnyObject>
105
+ }
106
+
107
+ public func objectOrNil(forKey key: String, fromDictionary dict: Dictionary<String, AnyObject>) -> AnyObject?
108
+ {
109
+ if let object: AnyObject = dict[key] {
110
+ if !(object is NSNull) {
111
+ return object
112
+ }
113
+ }
114
+ return nil
115
+ }
116
+
117
+ required public init(coder aDecoder: NSCoder) {
118
+ <% for @property in @properties %>
119
+ self.<%= @property.name %> = aDecoder.decodeObjectForKey(<%= @name %>.k<%= @name %><%= @property.name.capitalize %>)!<% if @property.type != 'AnyObject' %> as? <%= @property.type %><% end %>
120
+ <% end %>
121
+ }
122
+
123
+ public func encodeWithCoder(aCoder: NSCoder) {
124
+ <% for @property in @properties %>
125
+ aCoder.encodeObject(<%= @property.name %>, forKey:<%= @name %>.k<%= @name %><%= @property.name.capitalize %>)
126
+ <% end %>
127
+ }
128
+
129
+ override public var description: String {
130
+ get {
131
+ return "\(dictionaryRepresentation())"
132
+ }
133
+ }
134
+ }
@@ -1,6 +1,6 @@
1
1
  # Tranforms a JSON string into Objects
2
2
  module Jaspion
3
3
  module Kilza
4
- VERSION = '1.0.9'
4
+ VERSION = '1.1.0'
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jaspion-kilza
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.9
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshiro Sugii
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-07 00:00:00.000000000 Z
11
+ date: 2016-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -166,6 +166,8 @@ files:
166
166
  - lib/jaspion/kilza/language/objc.rb
167
167
  - lib/jaspion/kilza/language/objc/h.erb
168
168
  - lib/jaspion/kilza/language/objc/m.erb
169
+ - lib/jaspion/kilza/language/swift.rb
170
+ - lib/jaspion/kilza/language/swift/swift.erb
169
171
  - lib/jaspion/kilza/property.rb
170
172
  - lib/jaspion/kilza/source.rb
171
173
  - lib/jaspion/kilza/version.rb
@@ -190,7 +192,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
190
192
  version: '0'
191
193
  requirements: []
192
194
  rubyforge_project:
193
- rubygems_version: 2.4.6
195
+ rubygems_version: 2.4.8
194
196
  signing_key:
195
197
  specification_version: 4
196
198
  summary: Parses JSON and generates objects into other languages