lhj-tools 0.2.33 → 0.2.34

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
  SHA256:
3
- metadata.gz: ff6929383a9eb5b439ba528f359328c1794dea01bbe1716ba5a5561c30d70f22
4
- data.tar.gz: f6be4caa44459d4fc44bd0d2b11eef4601ef53df196250f6b20b59dd240be3bb
3
+ metadata.gz: 3cc00706a73b9e787e9d9336da8f7c8e5e787ff6e6192dff2383e65792561ec0
4
+ data.tar.gz: 83c616ccc6b192910102979f6c091d9e48b36938588391b963a659362dd0604a
5
5
  SHA512:
6
- metadata.gz: b27ba2d5508724e1b6c3325492cff864cf1243dea0a13d2abf325a0cd77fa8b9fbbdf690b8f1765625fe5ae8e708f62bc1e14e4df8b6e682d25cfc5c68b3df25
7
- data.tar.gz: ab0e21f4c76d6bdc9c1c1bcdf1699b4b098044631259a943dd18b963a9210df26518fc767b93e9cd62a2682111ff72b902a72586cd220ac94d4b50e456f299f5
6
+ metadata.gz: e1062fd7c95362b9c0cac73359276d929fe49b30643522f8fb11f4ef8f311fcc505974d934f703bacf29a16d29213906d80a97d96bacad99a467d9ead2c98943
7
+ data.tar.gz: 158f1213427ce173a39537f0b8187c7ad3de2992d4b7bdcf643e46a1d0ab04e478ae0aae25c2b0603e49d328522eaa19ef45ef4303e4ccc0e9592a51d57a7276
@@ -118,6 +118,8 @@ module Lhj
118
118
  @req_mock_array ||= []
119
119
  @req_mock_array << str
120
120
  when :java
121
+ when :swift
122
+ when :dart
121
123
  end
122
124
  end
123
125
 
@@ -176,6 +178,10 @@ module Lhj
176
178
  print_models_impl(models)
177
179
  when :java
178
180
  print_models_for_java(models)
181
+ when :swift
182
+ print_models_for_swift(models)
183
+ when :dart
184
+ print_models_for_dart(models)
179
185
  end
180
186
  puts "\n<===============打印返回数据模型-End=====================>\n".green
181
187
  models
@@ -196,6 +202,10 @@ module Lhj
196
202
  print_models_impl(models)
197
203
  when :java
198
204
  print_models_for_java(models)
205
+ when :swift
206
+ print_models_for_swift(models)
207
+ when :dart
208
+ print_models_for_dart(models)
199
209
  end
200
210
  puts "\n<===============打印请求模型-End=====================>\n".green
201
211
  models
@@ -252,7 +262,7 @@ module Lhj
252
262
  o = v['items'] || v
253
263
  o['name'] = gen_model_name(k, type)
254
264
  if v['type'].eql?('array') && v['items']['type'].eql?('string')
255
- c_model[:type_name] = 'NSString'
265
+ c_model[:type_name] = 'String'
256
266
  else
257
267
  c_model[:type_name] = o['name']
258
268
  handle_model(o, type, &block)
@@ -293,10 +303,109 @@ module Lhj
293
303
  end
294
304
  end
295
305
 
306
+ def print_models_for_swift(models)
307
+ models.each do |model|
308
+ model_name = model[:name] || ''
309
+ model_properties = model[:properties]
310
+ puts_h "class #{model_name}: NSObject {"
311
+ model_properties.each do |m|
312
+ print_model_for_swift(m)
313
+ end
314
+ puts_h "}\n\n\n"
315
+ end
316
+ end
317
+
318
+ def print_models_for_dart(models)
319
+ models.each do |model|
320
+ model_name = model[:name] || ''
321
+ model_properties = model[:properties]
322
+ puts_h "class #{model_name} {"
323
+ model_properties.each do |m|
324
+ print_model_for_dart(m)
325
+ end
326
+ # init method
327
+ puts_h "\n"
328
+ i_content = model_properties.map { |p| "this.#{p[:key]}" }.join(', ')
329
+ puts_h " #{model_name}({#{i_content}});"
330
+ puts_h "\n"
331
+ # json to model
332
+ puts_h " #{model_name}.fromJson(Map<String, dynamic> json) {"
333
+ puts_h json_to_model_dart(model_properties)
334
+ puts_h ' }'
335
+ # model to json
336
+ puts_h "\n"
337
+ puts_h ' Map<String, dynamic> toJson() {'
338
+ puts_h ' final Map<String, dynamic> data = <String, dynamic>{};'
339
+ puts_h model_to_json_dart(model_properties)
340
+ puts_h ' return data;'
341
+ puts_h ' }'
342
+ puts_h "}\n\n\n"
343
+ end
344
+ end
345
+
346
+ def json_to_model_dart(properties)
347
+ p_s = properties.map do |p|
348
+ case p[:type]
349
+ when 'object'
350
+ " #{p[:key]} = json['#{p[:key]}'] != null ? #{p[:type_name]}.fromJson(json['#{p[:key]}']) : null;"
351
+ when 'array'
352
+ if p[:type_name].eql?('String')
353
+ " #{p[:key]} = json['#{p[:key]}'].cast<String>();"
354
+ else
355
+ json_to_model_array_dart(p)
356
+ end
357
+ else
358
+ " #{p[:key]} = json['#{p[:key]}'];"
359
+ end
360
+ end
361
+ p_s.join("\n")
362
+ end
363
+
364
+ def json_to_model_array_dart(p)
365
+ array = []
366
+ array << " if (json['#{p[:key]}'] != null) {"
367
+ array << " #{p[:key]} = <#{p[:type_name]}>[];"
368
+ array << " json['#{p[:key]}'].forEach((v) {"
369
+ array << " #{p[:key]}!.add(#{p[:type_name]}.fromJson(v));"
370
+ array << ' });'
371
+ array << ' }'
372
+ array.join("\n")
373
+ end
374
+
375
+ def model_to_json_dart(properties)
376
+ p_s = properties.map do |p|
377
+ case p[:type]
378
+ when 'object'
379
+ model_to_model_obj_dart(p)
380
+ when 'array'
381
+ model_to_model_array_dart(p)
382
+ else
383
+ " data['#{p[:key]}'] = #{p[:key]};"
384
+ end
385
+ end
386
+ p_s.join("\n")
387
+ end
388
+
389
+ def model_to_model_obj_dart(p)
390
+ array = []
391
+ array << " if (#{p[:key]} != null) {"
392
+ array << " data['#{p[:key]}'] = #{p[:key]}!.toJson();"
393
+ array << ' }'
394
+ array.join("\n")
395
+ end
396
+
397
+ def model_to_model_array_dart(p)
398
+ array = []
399
+ array << " if (#{p[:key]} != null) {"
400
+ array << " data['#{p[:key]}'] = #{p[:key]}!.map((v) => v.toJson()).toList();"
401
+ array << ' }'
402
+ array.join("\n")
403
+ end
404
+
296
405
  def print_models_impl(models)
297
406
  models.each do |model|
298
407
  puts_m "@implementation #{model[:name]}"
299
- str = model[:properties].filter { |p| p[:type].eql?('array') && !p[:type_name].eql?('NSString') }.map { |p| "@\"#{p[:key]}\": #{p[:type_name]}.class" }.join(', ')
408
+ str = model[:properties].filter { |p| p[:type].eql?('array') && !p[:type_name].eql?('String') }.map { |p| "@\"#{p[:key]}\": #{p[:type_name]}.class" }.join(', ')
300
409
  if str&.length&.positive?
301
410
  puts_m '+(NSDictionary *)modelContainerPropertyGenericClass {'
302
411
  puts_m " return @{#{str}};"
@@ -376,6 +485,65 @@ module Lhj
376
485
  end
377
486
  end
378
487
 
488
+ def print_model_for_swift(m)
489
+ key = m[:key]
490
+ type_name = m[:type_name]
491
+ type = m[:type]
492
+ des = m[:description] || ''
493
+ des.gsub!(/\n/, ' ')
494
+ default = m[:default]
495
+ case type
496
+ when 'integer'
497
+ puts_h " @objc var #{key}: Int?//#{des} #{default}"
498
+ when 'string'
499
+ puts_h " @objc var #{key}: String?//#{des} #{default}"
500
+ when 'number'
501
+ puts_h " @objc var #{key}: Float?//#{des} #{default}"
502
+ when 'float'
503
+ puts_h " @objc var #{key}: Float?//#{des} #{default}"
504
+ when 'double'
505
+ puts_h " @objc var #{key}: Double?//#{des} #{default}"
506
+ when 'boolean'
507
+ puts_h " @objc var #{key}: Bool?//#{des} #{default}"
508
+ when 'object'
509
+ puts_h " @objc var #{key}: #{type_name}?//#{des} #{default}"
510
+ when 'array'
511
+ puts_h " @objc var #{key}: Array<#{type_name}>?//#{des} #{default}"
512
+ else
513
+ puts_h " @objc var #{key}: String?//#{des} #{default}"
514
+ end
515
+ end
516
+
517
+ def print_model_for_dart(m)
518
+ key = m[:key]
519
+ type_name = m[:type_name]
520
+ type = m[:type]
521
+ des = m[:description] || ''
522
+ des.gsub!(/\n/, ' ')
523
+ default = m[:default]
524
+ puts_h " //#{des} #{default}"
525
+ case type
526
+ when 'integer'
527
+ puts_h " int? #{key};"
528
+ when 'string'
529
+ puts_h " String? #{key};"
530
+ when 'number'
531
+ puts_h " int? #{key};"
532
+ when 'float'
533
+ puts_h " float? #{key};"
534
+ when 'double'
535
+ puts_h " double? #{key};"
536
+ when 'boolean'
537
+ puts_h " bool? #{key};"
538
+ when 'object'
539
+ puts_h " #{type_name}? #{key};"
540
+ when 'array'
541
+ puts_h " List<#{type_name}>? #{key};"
542
+ else
543
+ puts_h " String? #{key};"
544
+ end
545
+ end
546
+
379
547
  # @param [Object] data
380
548
  def print_req_query(data)
381
549
  return unless data && data['req_query']
@@ -394,6 +562,10 @@ module Lhj
394
562
  print_models_impl(models)
395
563
  when :java
396
564
  print_models_for_java(models)
565
+ when :swift
566
+ print_models_for_swift(models)
567
+ when :dart
568
+ print_models_for_dart(models)
397
569
  end
398
570
  models
399
571
  end
@@ -437,6 +609,8 @@ module Lhj
437
609
  yapi_temp_result = Lhj::ErbTemplateHelper.render(yapi_temp, yapi_vars, '-')
438
610
  puts_s(yapi_temp_result)
439
611
  when :java
612
+ when :swift
613
+ when :dart
440
614
  end
441
615
 
442
616
  end
data/lib/lhj/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lhj
4
- VERSION = '0.2.33'
4
+ VERSION = '0.2.34'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhj-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.33
4
+ version: 0.2.34
5
5
  platform: ruby
6
6
  authors:
7
7
  - lihaijian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-05 00:00:00.000000000 Z
11
+ date: 2023-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xcodeproj