Skip to content

Commit 03ba838

Browse files
author
Sven
committed
feat: YApi 通过Id转模支持。
1 parent cdbbe00 commit 03ba838

File tree

7 files changed

+46
-22
lines changed

7 files changed

+46
-22
lines changed

README.md

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
SwiftJSONModeler是一个Xcode插件,可以将json 转为模型
1+
SwiftJSONModeler是一个Xcode插件,可以将json 转为Swfit模型
22
* 支持struct, class
3-
* 支持标准单json和YApi RAW(自动为你添加注释)
3+
* 支持单json转模
4+
* 支持YApi RAW或接口id解析转模,并且自动添加注释
45
* 支持自定义遵循 和 import
56
* 支持自定义模型前缀和后缀
67
* 可设置是否类型可选,是否使用显示可选`?`(不使用则为隐式可选`!`)
@@ -13,15 +14,16 @@ SwiftJSONModeler是一个Xcode插件,可以将json 转为模型
1314
复制单json, 一步转为模型。
1415

1516
![运行效果](./Sources/example.gif)
16-
如果无法预览查看Sources/example.gif
17+
如果无法预览查看[Sources/example.gif](./Sources/example.gif)
1718

1819
### YApi RAW解析
1920

20-
复制YApi接口RAW数据,一步解析模型。如果配置为继承和引入HandyJSON, 添加JD模型前缀和Model后缀,解析效果如下:
21+
复制YApi接口RAW数据,一步解析带有注释的模型。
22+
解析效果如下():
2123

2224
![解析YApi](./Sources/YApiRAW.gif)
2325

24-
如果无法预览查看Source/YApiRAW.gif
26+
如果无法预览查看[Source/YApiRAW.gif](./Sources/YApiRAW.gif)
2527

2628
## 安装
2729
1.你可以选择[Release](https://github.com/yumengqing/SwiftJSONModeler/releases) 直接下载应用安装
@@ -41,29 +43,36 @@ SwiftJSONModeler是一个Xcode插件,可以将json 转为模型
4143

4244
> Tip:如果重启Xcode之后在Editor中还没看见插件选项,请选择系统设置-> 扩展->Xcode Source Editor中对应插件是否导入
4345
44-
### 设置快捷键
46+
## 设置
47+
SwiftJSONModeler提供多种自定义可选设置,可通过插件的Config选项进行设置。
4548

46-
可以给插件设置快捷键,快速转换模型
49+
设置选项如下图:
4750

48-
在Xcode -> Preference -> Key Bindings -> Editor Menu For Source Code【或者搜索】就可以找到,如下图
51+
![可选设置](./Sources/config.png)
4952

50-
![](./Sources/keybinding.png)
53+
**YApi模型路径指定**:当对象含有多层json,或者有基本json包裹时,可以指定path解析模型。
5154

52-
记得双击key下面那个区域才可以编辑,这里我使用的是alt + s 和alt + c 避免与系统的冲突
53-
### Model设置
54-
模型设置提供一些自定义控制,具体如下图:
55-
![Alt](./Sources/config.png)
56-
### 自定义操作confrom 和import
55+
**YApi项目token**: 每个YApi项目都有唯一一个token,在YApi项目接口的设置中可以查看对应的token,如下图:
56+
57+
![token](./Sources/yapiToken.png)
58+
59+
**YApi项目host**:host就是部署YApi的地址
5760

58-
可以通过点击应用或者选择扩展的config选项,自定义填写import模块和继承
61+
![host](./Sources/yapiHostId.png)
5962

60-
比如我需要继承HandyJSON
63+
## 设置快捷键
6164

62-
![自定义遵循](./Sources/customConfig.png)
65+
可以给插件设置快捷键,快速转换模型
66+
67+
在Xcode -> Preference -> Key Bindings -> Editor Menu For Source Code【或者搜索】就可以找到,如下图
68+
69+
![](./Sources/keybinding.png)
6370

71+
记得双击key下面那个区域才可以编辑,这里我使用的是alt + s 和alt + c 避免与系统的冲突
6472
## 标准JSON转模
6573

66-
使用的使用,首先要选中需要转换的json,注意不标准的json会报错的哟。仅限单层json,多层json子json需要单独转换
74+
复制需要转换的json,选择插件的Struct JSON或者Class JSON功能,即可生成相应Swift模型。
75+
注意:目前只支持单层json
6776

6877
示例json
6978

@@ -104,7 +113,17 @@ struct <#Model#>: HandyJSON {
104113

105114
## YApi 接口平台转模
106115

107-
如果你使用YApi接口平台,可以通过复制接口返回的 RAW数据,自动转为模型,并且自动根据YApi为模型添加注释。
116+
如果你使用YApi接口平台,可以通过复制接口返回的 RAW数据,或者复制Id,自动转为模型,并且自动根据YApi为模型添加注释。
117+
118+
### 通过Id转模
119+
120+
在配置了项目token和host基础下,简单复制Id即可实现转模和添加注释.
121+
122+
**如何查看YApi接口中的id?**在YApi对应的接口中,查看浏览器网址,最后的数字就是Id。如下图:
123+
124+
![接口Id](./Sources/yapiHostId.png)
125+
126+
108127

109128
### RAW数据
110129

@@ -114,15 +133,17 @@ struct <#Model#>: HandyJSON {
114133

115134
示例RAW数据:
116135

117-
```swift
118-
{"type":"object","title":"empty object","properties":{"person":{"type":"object","properties":{"name":{"type":"string","description":"姓名","mock":{"mock":"小明"}},"age":{"type":"integer","description":"年龄","mock":{"mock":"12"}},"school":{"type":"object","properties":{"address":{"type":"string","mock":{"mock":"xx街道xx号"},"description":"学校地址"},"schoolName":{"type":"string","description":"学校名字"}},"required":["address","schoolName"],"description":"学校"},"likes":{"type":"array","items":{"type":"string"},"description":"好友"},"teachers":{"type":"array","items":{"type":"object","properties":{"name":{"type":"string","description":"老师名字"},"subject":{"type":"string","description":"科目","mock":{"mock":"语文"}},"isMale":{"type":"boolean","description":"是否为男"}},"required":["name","subject","isMale"]}}},"required":["name","age","school","likes","teachers"],"description":"个人信息详情"}},"required":["person"]}
136+
```javascript
137+
{"type":"object","title":"empty object","properties":{"message":{"type":"string"},"code":{"type":"string"},"response":{"type":"object","properties":{"teachers":{"type":"array","items":{"type":"object","properties":{"name":{"type":"string","mock":{"mock":"Mrs Yang"},"description":"名字"},"subject":{"type":"string","mock":{"mock":"语文"},"description":"科目"},"phone":{"type":"string","mock":{"mock":"13459923098"},"description":"联系电话"}},"required":["name","subject","phone"]},"description":"老师"},"name":{"type":"string","description":"姓名"},"age":{"type":"integer","mock":{"mock":"18"},"description":"年龄"},"score":{"type":"number","mock":{"mock":"89.8"},"description":"综合成绩"},"likes":{"type":"array","items":{"type":"string","mock":{"mock":"英雄联盟"}},"description":"爱好"},"emergercyContact":{"type":"object","properties":{"name":{"type":"string"},"phone":{"type":"string","description":"联系电话"},"address":{"type":"string","description":"联系地址","mock":{"mock":"xx街道xx栋xx单元"}}},"description":"紧急联系人","required":["name","phone","address"]},"isBoy":{"type":"boolean","description":"是否为男孩"}},"required":["teachers","name","age","score","likes","emergercyContact","isBoy"]}},"required":["message","code","response"]}
119138
```
120139

121140
> 完整示例可下载打开SwiftJSONModelerDemo查看
122141
123142
### 解析指定路径模型
124143

125-
如果你数据有多层json, 可以在设置中指定解析路线path 比如想解析json data属性下的,则在path中添加`data`,也可以解析多层路径比如`data.person`
144+
如果你数据有多层json, 可以在设置中指定解析路线path来获取指定模型。比如我的目标数据在response字段下,则可以配置path为response.则直接解析response下的json模型。多路径使用 `.`
145+
146+
126147

127148
## 待优化
128149

Sources/YApiRAW.gif

-31.5 MB
Loading

Sources/config.png

37.4 KB
Loading

Sources/customConfig.png

-54.4 KB
Binary file not shown.

Sources/yapiHostId.png

27.2 KB
Loading

Sources/yapiToken.png

149 KB
Loading

SwiftJSONModeler/SourceEditorCommand.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
4949
let commandIdentifier = invocation.commandIdentifier
5050
if commandIdentifier == configCommand {
5151
NSWorkspace.shared.open(URL(fileURLWithPath: "/Applications/SwiftJSONModeler For Xcode.app"))
52+
completionHandler(nil)
5253
} else if commandIdentifier == classFromRAWCommand || commandIdentifier == structFromRAWCommand {
5354
handleInvocation(invocation, raw: pasteboardTest, completionHandler: completionHandler)
5455

@@ -58,6 +59,8 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
5859
YApiRequest.data(id: pasteboardTest) { [weak self](raw) in
5960
if let raw = raw {
6061
self?.handleInvocation(invocation, raw: raw, completionHandler: completionHandler)
62+
}else {
63+
completionHandler(nil)
6164
}
6265
}
6366
}

0 commit comments

Comments
 (0)