jsoncop 0.3.1 → 0.4.1

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: 4661699526bb0f0bc7066755875aa0cdf87f3005
4
- data.tar.gz: 4185ad9d96938c2dd3ac93e91767f2faca424c53
3
+ metadata.gz: 7784c2e3c647143607c371e7253421fcbd54848c
4
+ data.tar.gz: e62a4673e734dc2a25650d1800cf7ccfa1a917a9
5
5
  SHA512:
6
- metadata.gz: 18d32c6cb4a60cfac861e91a5a40dc2c6120b2884ddaf1eaa623bc223718a77724b93b9e4d6adbef8b984a152218c49bb5efa53b6892d333a738e52586d6402f
7
- data.tar.gz: bfa0bd079a97d783ade970ec58f074b5edf16b74567227aa825c61593829e48ec4aa395b0fb0387ab1caf79e358e75eae226d990d9ef3fe7f6e374da2b27a01c
6
+ metadata.gz: d0d47f9d3493cf8a5b9124a9b16a44388e25c9b7f06bc103cea4f2f665a99393cf34cf831e3a06de764d69ce63beb420c336022158e9847a9e461fd4449f1198
7
+ data.tar.gz: 5e3bcbb3012ff57a07b3d7dcf82f65d096b0f86b046e8eb9df055e9c2a0e8958c7fc23bea506a2fc7499cca83e36aca3fe06933057a71c29807f2fd9b3ee90ea
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ ![](./images/jsoncop-banner.png)
2
+
1
3
  # JSONCop
2
4
 
3
5
  [![License](https://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://github.com/draveness/jsoncop/blob/master/LICENSE)
@@ -19,13 +21,13 @@ let person = Person.parse(json: json)
19
21
 
20
22
  ## Usage
21
23
 
22
- 1. Install JSONCop with `sudo gem install jsoncop --verbose` below in system ruby version
23
- 2. Run `cop install` in project root folder
24
+ 1. Install JSONCop with `sudo gem install jsoncop --verbose` below in **system ruby version**
25
+ 2. Run `cop install` in **project root folder**
24
26
  3. **Add `//@jsoncop` just before model definition line**
25
27
 
26
28
  ```shell
27
29
  $ sudo gem install jsoncop --verbose
28
- ~/project $ cop install
30
+ $ cop install
29
31
  ```
30
32
 
31
33
  ```swift
@@ -70,7 +72,7 @@ Checkout [JSONCopExample](./JSONCopExample) for more information.
70
72
  let name: String
71
73
 
72
74
  static func JSONKeyPathByPropertyKey() -> [String: String] {
73
- return ["nickname": "name"]
75
+ return ["name": "nickname"]
74
76
  }
75
77
  }
76
78
  ```
@@ -99,6 +101,14 @@ Checkout [JSONCopExample](./JSONCopExample) for more information.
99
101
  }
100
102
  ```
101
103
 
104
+ + Nested JSON value extraction
105
+
106
+ ```swift
107
+ static func JSONKeyPathByPropertyKey() -> [String: String] {
108
+ return ["currentProjectName": "project.name"]
109
+ }
110
+ ```
111
+
102
112
  ## Installation
103
113
 
104
114
  ```shell
@@ -11,7 +11,7 @@ module JSONCop
11
11
  MODEL_NAME_REGEX = /(struct|class)\s+(.+)\s*{/
12
12
  ATTRIBUTE_REGEX = /^\s+(let|var)\s(.+):(.+)/
13
13
  JSON_TRANSFORMER_REGEX = /^\s+static\s+func\s+(.+)JSONTransformer\(.+?\:(.+)\).+->.+/
14
- JSON_BY_PROPERTY_HASH_REGEX = /static\s+func\s+JSONKeyPathByPropertyKey\(\)\s*->\s*\[String\s*:\s*String\]\s*{\s*return\s*(\[[\s"a-z0-9A-Z_\-:\[\],]*)}/
14
+ JSON_BY_PROPERTY_HASH_REGEX = /static\s+func\s+JSONKeyPathByPropertyKey\(\)\s*->\s*\[String\s*:\s*String\]\s*{\s*return\s*(\[[\s\."a-z0-9A-Z_\-:\[\],]*)}/
15
15
 
16
16
  attr_reader :file_path
17
17
  attr_accessor :current_model
@@ -43,10 +43,9 @@ module JSONCop
43
43
  if json_attr_list
44
44
  json_attr_list.gsub!(/[(\[\]"\s)]*/, '')
45
45
  @current_model.attr_json_hash = Hash[json_attr_list.split(",").map {
46
- |attr_json_pair| attr_json_pair.split(":").reverse
46
+ |attr_json_pair| attr_json_pair.split(":")
47
47
  }]
48
48
  end
49
-
50
49
  end
51
50
 
52
51
  models
@@ -14,10 +14,10 @@ module JSONCop
14
14
 
15
15
  def generate!
16
16
  jsoncop_generate_start = /\/\/ MARK: \- JSONCop\-Start/
17
- jsoncop_generate_end = /\/\/ MARK: \- JSONCop\-End/
17
+ jsoncop_generate_end = /\/\/ JSONCop\-End/
18
18
  content = File.read file_path
19
19
  if content.match(jsoncop_generate_start) && content.match(jsoncop_generate_end)
20
- content.gsub!(/\/\/ MARK: \- JSONCop\-Start[^$]*MARK: \- JSONCop\-End/, json_cop_template)
20
+ content.gsub!(/\/\/ MARK: \- JSONCop\-Start[^$]*JSONCop\-End/, json_cop_template)
21
21
  else
22
22
  content += json_cop_template
23
23
  end
@@ -56,18 +56,33 @@ JSONCOP
56
56
  #
57
57
  # CODING
58
58
  end
59
- result += "// MARK: - JSONCop-End"
59
+ result += "// JSONCop-End"
60
60
  result
61
61
  end
62
62
 
63
63
  def json_parsing_template(model)
64
64
  model.attributes.map do |attr|
65
65
  transformer = model.transformers.select { |t| t.name == attr.name }.first
66
- if transformer
67
- "let #{attr.name} = (json[\"#{model.attr_json_hash[attr.name] || attr.name}\"] as? #{transformer.type}).flatMap(#{attr.name}JSONTransformer)"
68
- else
69
- "let #{attr.name} = json[\"#{model.attr_json_hash[attr.name] || attr.name}\"] as? #{attr.type}"
66
+ attr_key_path = model.attr_json_hash[attr.name] || attr.name
67
+ return unless attr_key_path
68
+ value = "json"
69
+ key_paths = attr_key_path.split(".")
70
+ key_paths.each_with_index do |key, index|
71
+ value.insert 0, "("
72
+ value += "[\"#{key}\"] as? "
73
+ if index == key_paths.count - 1
74
+ if transformer
75
+ value += "#{transformer.type})"
76
+ value += ".flatMap(#{attr.name}JSONTransformer)"
77
+ else
78
+ value += "#{attr.type})"
79
+ end
80
+ else
81
+ value += "[String: Any])?"
82
+ end
70
83
  end
84
+
85
+ "let #{attr.name} = #{value}"
71
86
  end.join(",\n\t\t\t")
72
87
  end
73
88
 
@@ -3,8 +3,8 @@ module JSONCop
3
3
  class Attribute
4
4
  attr_reader :name, :type
5
5
  def initialize(name, type)
6
- @name = name.gsub(/\s+/, "")
7
- @type = type.gsub(/\s+/, "")
6
+ @name = name.gsub(/\s+/, "")
7
+ @type = type.gsub(/\s+/, "")
8
8
  end
9
9
 
10
10
  def real_type
@@ -1,3 +1,3 @@
1
1
  module JSONCop
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsoncop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Draveness
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-20 00:00:00.000000000 Z
11
+ date: 2016-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: claide