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 +4 -4
- data/README.md +14 -4
- data/lib/jsoncop/analyzer.rb +2 -3
- data/lib/jsoncop/generator.rb +22 -7
- data/lib/jsoncop/model/attribute.rb +2 -2
- data/lib/jsoncop/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7784c2e3c647143607c371e7253421fcbd54848c
|
4
|
+
data.tar.gz: e62a4673e734dc2a25650d1800cf7ccfa1a917a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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 ["
|
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
|
data/lib/jsoncop/analyzer.rb
CHANGED
@@ -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(":")
|
46
|
+
|attr_json_pair| attr_json_pair.split(":")
|
47
47
|
}]
|
48
48
|
end
|
49
|
-
|
50
49
|
end
|
51
50
|
|
52
51
|
models
|
data/lib/jsoncop/generator.rb
CHANGED
@@ -14,10 +14,10 @@ module JSONCop
|
|
14
14
|
|
15
15
|
def generate!
|
16
16
|
jsoncop_generate_start = /\/\/ MARK: \- JSONCop\-Start/
|
17
|
-
jsoncop_generate_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[^$]*
|
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 += "//
|
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
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|
|
data/lib/jsoncop/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2016-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: claide
|