gwtools 0.0.6 → 0.0.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 623dae2a8da9444e088fb0775b8490beb07a339d1b48e040b28be4c955fb30b1
4
- data.tar.gz: 4488cddef5c3b1e72e992f263d38cd23e40951731cc5218aa95e1afb31fb2dd6
3
+ metadata.gz: 001eef35553a92118abccc74a73ef133fce57509e775e9cb7361b014a2bc6436
4
+ data.tar.gz: 35ed21f791aee545295149c39b747b789d02057616b7c0e747861e9fb6f9ff14
5
5
  SHA512:
6
- metadata.gz: c52c6bffb2ff6816c726ab1c6d13fc7430e8d2e7b07c8005683a18b0da6dd7a1c3a9d8b49251234f4285067b7cb3aab92df6255e01297143ce6c942f253d2338
7
- data.tar.gz: 46e363b7424217396fc8ca484850eea5a9b4a6a7cc7042adf8a812c4203f1943a13c843bfa16104c4931b8ce15fa579820efa2ee56b27540a9a965b3a5570b4c
6
+ metadata.gz: dd259aa0fde7b6c9c958b5b49b8bd83dc70d4cfde0dc3eafea2265f1b6976a657ebd4beee4c559b866e25e133e02d58b704312babed58cfac442bf010c78d229
7
+ data.tar.gz: 2e8e27d43e284ccf7301d0ce5eff6b3e9b49cd18908ce32e4c87dadded65104da415d5cb8bb753b1b87f5992e58020278f4a0dd3d08e8f806970f797b77d5a38
@@ -57,7 +57,7 @@ module Gwtools
57
57
  end
58
58
 
59
59
  # create file
60
- Generate::Swift.createPathFile(dir_path, dir, dirs, index, @httpMethod, @responseJson)
60
+ Generate::Swift.createPathFile(dir_path, dir, dirs, index, @httpMethod, @responseJson, path)
61
61
  end
62
62
  end
63
63
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'erb'
4
4
  require 'json'
5
+ require 'pp'
5
6
 
6
7
  module Gwtools
7
8
  class Generate
@@ -37,7 +38,7 @@ extension <%="#{@api_name}"%> {
37
38
 
38
39
  class ModelReqGenerateConfig
39
40
  include(ERB::Util)
40
- attr_accessor :api_name, :httpmethod, :prefixPath, :modulePath, :detailPath, :model_name, :model_data
41
+ attr_accessor :api_name, :httpmethod, :prefixPath, :modulePath, :detailPath, :model_name, :model_data, :json_data_is_arr
41
42
 
42
43
  # 将 erb 文件的一个实例化,绑定到当前的一个对象的上下文
43
44
  def config_file
@@ -60,7 +61,11 @@ public extension <%="#{@api_name}"%> {
60
61
  isCache: Bool = false,
61
62
  isLoading: Bool = false,
62
63
  module: String? = nil,
63
- des: String? = nil) -> Observable<Self.<%="#{@httpmethod}"%><%="#{@model_name}"%>> {
64
+ <% if #{@json_data_is_arr} %>
65
+ des: String? = nil) -> Observable<[Self.<%="#{@model_name}"%>]> {
66
+ <% else %>
67
+ des: String? = nil) -> Observable<Self.<%="#{@model_name}"%>> {
68
+ <% end %>
64
69
  // ================= 逻辑处理 =================
65
70
  let headerParms: [String : String] = [:]
66
71
  let parms: [String : Any] = [:]
@@ -69,22 +74,25 @@ public extension <%="#{@api_name}"%> {
69
74
  return GWNetworkManager.request(requestType: .<%="#{@httpmethod}"%>,
70
75
  prefixPath: "<%="#{@prefixPath}"%>",
71
76
  modulePath: "<%="#{@modulePath}"%>",
72
- detailPath: "<%="#{@detailPath}"%>",
77
+ detailPath: "<%="#{@detailPath}"%>",
73
78
  parms: parms,
74
79
  headerParms: headerParms,
75
80
  isCache: isCache,
76
81
  isLoading: isLoading,
77
82
  module: module,
78
83
  des: des)
79
- .mapHandyJSON(Self.<%="#{@model_name}"%>.self)
80
-
84
+ <% if #{@json_data_is_arr} %>
85
+ .mapHandyJSON2Array(Self.<%="#{@model_name}"%>.self)
86
+ <% else %>
87
+ .mapHandyJSON(Self.<%="#{@model_name}"%>.self)
88
+ <% end %>
81
89
  }
82
90
  }
83
91
  ERB
84
92
  ERB.new(template, nil, '>').result(binding)
85
93
  end
86
94
 
87
- def initialize(file_path, api_name, httpmethod, prefixPath, modulePath, detailPath, model_name, model_data)
95
+ def initialize(file_path, api_name, httpmethod, prefixPath, modulePath, detailPath, model_name, model_data, json_data_is_arr)
88
96
  @api_name = api_name
89
97
  @httpmethod = httpmethod
90
98
  @prefixPath = prefixPath
@@ -92,6 +100,7 @@ public extension <%="#{@api_name}"%> {
92
100
  @detailPath = detailPath
93
101
  @model_name = model_name
94
102
  @model_data = model_data
103
+ @json_data_is_arr = json_data_is_arr
95
104
 
96
105
  File.open(file_path, 'w+') do |f|
97
106
  f.puts(config_file)
@@ -99,43 +108,89 @@ public extension <%="#{@api_name}"%> {
99
108
  end
100
109
  end
101
110
 
102
- def self.createPathFile(dir_path, dir, dirs, index, http_method, response_json)
103
- if index <= 0
104
- return
111
+ def self.getPrefixPath(dir_path)
112
+ prefixPaths = %w[app-api/api/v1.0 app-api/api/v2.0]
113
+
114
+ prefixPaths.each do |prefix_path|
115
+ return prefix_path.gsub(/^\/|\/$/, "") if dir_path.start_with?(prefix_path)
116
+ end
117
+
118
+ return ''
119
+ end
120
+
121
+ def self.getModulePath(dir_path, prefixPath)
122
+ modulePaths = %w[content community]
123
+ dir_path = dir_path.gsub(/\A#{prefixPath}/, "").gsub(/^\/|\/$/, "")
124
+ modulePaths.each do |modulePath|
125
+ return modulePath if dir_path.start_with?(modulePath)
126
+ end
127
+
128
+ return ''
129
+ end
130
+
131
+ def self.getDetailPath(dir_path, prefixPath, modulePath)
132
+ return dir_path.gsub(/^\/|\/$/, "").gsub(/\A#{prefixPath}/, "")
133
+ .gsub(/^\/|\/$/, "").gsub(/\A#{modulePath}/, "")
134
+ .gsub(/^\/|\/$/, "")
135
+ end
136
+
137
+ def self.createPathFile(dir_path, dir, dirs, index, http_method, response_json, url_path)
138
+ app_name = "AppAPI.#{dirs[0..index-1].join('.')}"
139
+ if index == 0
140
+ app_name = "AppAPI"
105
141
  end
106
142
  if dir == 'restful'
107
- puts dir_path
108
- puts dir
109
- puts dirs.join('/')
110
- puts dirs[0..index-1].join('.')
111
- swift_model = generate_model("Model", response_json)
143
+ swift_model = generate_model("#{http_method.capitalize.chomp}Model", response_json)
144
+ prefixPath = getPrefixPath(url_path)
145
+ modulePath = getModulePath(url_path, prefixPath)
146
+ detailPath = getDetailPath(url_path, prefixPath, modulePath)
112
147
  ModelReqGenerateConfig.new(
113
148
  "#{dir_path}.swift",
114
- "AppAPI.#{dirs[0..index-1].join('.')}",
115
- http_method.capitalize,
116
- 'app-api/api/v1.0',
117
- 'content',
118
- '/route/getContentInfo',
119
- 'GetModel',
120
- swift_model
149
+ app_name,
150
+ http_method.capitalize.chomp,
151
+ prefixPath,
152
+ modulePath,
153
+ detailPath,
154
+ "#{http_method.capitalize.chomp}Model",
155
+ swift_model,
156
+ @json_data_is_arr
121
157
  )
122
158
 
123
159
  `swiftformat --swiftversion 5.0 #{dir_path}.swift`
124
160
  else
161
+
125
162
  PathGenerateConfig.new(
126
163
  "#{dir_path}.swift",
127
- "AppAPI.#{dirs[0..index-1].join('.')}",
164
+ app_name,
128
165
  dir
129
166
  )
130
167
  end
131
168
 
132
169
  end
133
170
 
134
- def self.generate_model(name, json_data)
135
- swift_model = "struct #{name}: HandyJson {\n"
171
+ def self.generate_model(name, json_data, level = 0)
172
+
136
173
  if json_data == nil
137
174
  return ''
138
175
  end
176
+
177
+ if level == 0
178
+ # 找到 真正要处理的 data 数据
179
+ json_data.each do |key, value|
180
+ if key == 'data'
181
+ case value
182
+ when Array
183
+ json_data = value.first
184
+ @json_data_is_arr = true
185
+ when Hash
186
+ json_data = value
187
+ @json_data_is_arr = false
188
+ end
189
+ end
190
+ end
191
+ end
192
+
193
+ swift_model = "struct #{name}: HandyJSON {\n"
139
194
  json_data.each do |key, value|
140
195
  case value
141
196
  when NilClass # nil 对象默认给个Stirng?类型
@@ -151,25 +206,22 @@ public extension <%="#{@api_name}"%> {
151
206
  when String
152
207
  swift_model += " var #{key}: String?\n"
153
208
  when Array
154
- swift_model += " var #{key}: [#{key.capitalize}] = []\n"
155
- swift_model += generate_model("#{key.capitalize}", value.first)
209
+ if !value.empty?
210
+ swift_model += " var #{key}: [#{key.capitalize}] = []\n"
211
+ swift_model += generate_model("#{key.capitalize}", value.first, level+1)
212
+ else
213
+ swift_model += " var #{key}: [Any] = []\n"
214
+ end
156
215
  when Hash
157
216
  swift_model += " var #{key}: #{key.capitalize}?\n"
158
- swift_model += generate_model("#{key.capitalize}", value)
217
+ swift_model += generate_model("#{key.capitalize}", value, level+1)
159
218
  else
160
219
  swift_model += " var #{key}: #{value.class == String ? 'String' : 'Int'}\n"
161
220
  end
162
221
  end
163
- swift_model + "}\n"
222
+ swift_model + "public init() {}\n}\n"
164
223
  end
165
224
 
166
- # def self.createModelAndRequest(target_path, responseJson)
167
- # swift_model = generate_model("Model", responseJson)
168
- # File.write(target_path + 'Model.swift', swift_model)
169
- # end
170
-
171
- # 生成 Swift 模型
172
-
173
225
  end
174
226
  end
175
227
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Gwtools
4
- VERSION = "0.0.6"
4
+ VERSION = "0.0.7"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gwtools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - chenglq