lib/lhj/command/yapi.rb in lhj-tools-0.1.79 vs lib/lhj/command/yapi.rb in lhj-tools-0.1.80
- old
+ new
@@ -62,17 +62,19 @@
# get project info
project_id = res_body['data']['project_id']
project_info = get_project_info(project_id)
# 1.print request result
- print_res_body_model(res_body)
+ res_models = print_res_body_model(res_body)
# 2.print request json body
- print_req_body_model(res_body) if res_body['data']['req_body_is_json_schema']
+ req_models = print_req_body_model(res_body) if res_body['data']['req_body_is_json_schema']
# 3.print request param
print_req_query(res_body['data']) unless res_body['data']['req_body_is_json_schema']
# 4.print request method
service_code = print_http_method(res_body['data'], project_info)
+ # print request mock data
+ print_mock_request_data(req_models) unless req_models.empty?
# 5.save to file
file_map = save_to_file(service_code) if @save
# 6.push to git
push_to_git if @sync
# 7.notify robot
@@ -235,40 +237,42 @@
res_body = fetch_res_boy(res_json)
return unless res_body
puts "\n<===============打印返回数据模型-Begin=====================>\n".green
models = []
- handle_model(res_body) do |model|
+ handle_model(res_body, :res) do |model|
models << model
end
case @language
when 'oc'
print_models(models)
print_models_impl(models)
when 'java'
print_models_for_java(models)
end
puts "\n<===============打印返回数据模型-End=====================>\n".green
+ models
end
def print_req_body_model(res_json)
req_body = fetch_req_body(res_json)
return unless req_body
puts "\n<===============打印请求模型-Begin=====================>\n".green
models = []
- handle_model(req_body) do |model|
+ handle_model(req_body, :req) do |model|
models << model
end
case @language
when 'oc'
print_models(models)
print_models_impl(models)
when 'java'
print_models_for_java(models)
end
puts "\n<===============打印请求模型-End=====================>\n".green
+ models
end
def fetch_res_boy(res_json)
return if !res_json || !res_json['data'] || !res_json['data']['res_body']
@@ -302,11 +306,11 @@
else
"#{model_pre}#{name}#{model_suffix}"
end
end
- def handle_model(model, &block)
+ def handle_model(model, type, &block)
p_type = model['type']
p_name = model['name']
p_properties = model['properties']
p_model = { name: p_name }
@@ -316,26 +320,26 @@
p_properties.each do |k, v|
c_type = @type_trans[v['type']] || v['type']
c_model = { key: k, type: c_type, description: v['description'], default: '' }
if v['type'].eql?('object') || v['type'].eql?('array')
o = v['items'] || v
- o['name'] = gen_model_name(k)
+ o['name'] = gen_model_name(k, type)
if v['type'].eql?('array') && v['items']['type'].eql?('string')
c_model[:type_name] = 'NSString'
else
c_model[:type_name] = o['name']
- handle_model(o, &block)
+ handle_model(o, type, &block)
end
end
properties << c_model
end
p_model[:properties] = properties
block[p_model] if block_given?
when 'array'
t = model['items']
t['name'] = p_name
- handle_model(t, &block)
+ handle_model(t, type, &block)
end
end
def print_models(models)
models.each do |model|
@@ -503,9 +507,40 @@
puts yapi_temp_result.green
return yapi_temp_result
when 'java'
end
+ end
+
+ def print_mock_request_data(req_models)
+ puts "\n<===============打印Mock Data-Begin=====================>\n".green
+ req_models.each do |model|
+ model_name = model[:name] || ''
+ model_properties = model[:properties]
+ puts "#{model_name} *mock = [[#{model_name} alloc] init];"
+ model_properties.each do |m|
+ puts "/// #{m[:description]}"
+ type = m[:type]
+ case type
+ when 'integer'
+ puts "mock.#{m[:key]} = 0;"
+ when 'number'
+ puts "mock.#{m[:key]} = 0;"
+ when 'float'
+ puts "mock.#{m[:key]} = 0;"
+ when 'double'
+ puts "mock.#{m[:key]} = 0;"
+ when 'object'
+ puts "mock.#{m[:key]} = @{};"
+ when 'array'
+ puts "mock.#{m[:key]} = @[];"
+ else
+ puts "mock.#{m[:key]} = @\"\";"
+ end
+ end
+ puts "\n\n"
+ end
+ puts "\n<===============打印Mock Data-End=====================>\n".green
end
end
end
end