如下这种针对 dns record 的操作, 因为增删改查操作不同, 导致 server 回复的数据不同, 针对这种情况, 反序列化的结构体要复用一个,使用枚举,可选字段之类的机制. 还是针对增删改查, 都定义单独的反序列化结构体?
一般常见的实践是怎么样的, 方便的话提供下理由就更好了, 谢谢.
{
"request":{
"operation":"dnsAddRecord",
"ip":"55.555.55.55"
},
"reply":{
"code":300,
"detail":"success",
"record_id":"1a2b3c4d5e"
}
}
{
"request":{
"operation":"dnsDeleteRecord",
"ip":"55.555.55.55"
},
"reply":{
"code":300,
"detail":"success"
}
}
{
"request":{
"operation":"dnsListRecords",
"ip":"55.555.55.55"
},
"reply":{
"code":300,
"detail":"success",
"resource_record":[
{
"record_id":"1a2b3c4d5e6f",
"type":"A",
"host":"test.namesilo.com",
"value":"55.555.55.55",
"ttl":"7207",
"distance":"0"
}
{
"record_id":"5Brg5hw25jr",
"type":"CNAME",
"host":"dev.namesilo.com",
"value":"testing.namesilo.com",
"ttl":"7207",
"distance":"0"
}
{
"record_id":"fH35aH4hsv",
"type":"MX",
"host":"namesilo.com",
"value":"mail.namesilo.com",
"ttl":"7207",
"distance":"10"
}
]
}
}
1
finab 24 天前
我反正这样写
``` struct ReplyModel { var code:Int var detail:String var record_id:String? var resource_record: [RecordModel]? var type: ReplyType { get { if record_id !=nil { return .... } } } } ``` |
2
finab 24 天前
enum ReplyType {
case id(record_id:String) case record(resource_record: [RecordModel]) } |