功能:model = modelType.transform(rawdata)

使用分析:

使用机制:继承+实现配置+使用;

需要自己实现什么?

设计分析:

工具模块?机制模块?model基类?

生成机制?

如何组织?

接口:通过继承使用接口;

主干类:

JSONDeserializer<T: HandyJSON>

HandyJSON: _ExtendCustomModelType: _Transformable: _Measurable

_ExtendCustomModelType

mutating func mapping(mapper: HelpingMapper)

mutating func didFinishMapping()

_Transformable

transform(from object: Any) -> Self?

_Measurable

headPointerOfStruct() -> UnsafeMutablePointer<Byte>

问题分解:

1、遍历rawdata对model赋值;

2、赋值解决方案,前提条件:无法获取成员变量的内存引用;

解决方案:kvc赋值(不支持)、根据内存对齐规则直接对内存赋值;

3、对象内存模型信息提取;

4、复杂赋值要处理的情况:

(1)rawdata引用与目标变量名称不一致;

(2)rawdata引用与目标变量路径不一致;

(3)rawdata引用与目标变量类型不一致;

static func _transform(dict: [String: Any], to instance: inout Self) {

guard let properties = getProperties(forType: Self.self) else {

InternalLogger.logDebug("Failed when try to get properties from type: \(type(of: Self.self))")

return

}

// do user-specified mapping first

let mapper = HelpingMapper()

instance.mapping(mapper: mapper)

// get head addr

let rawPointer = instance.headPointer()

InternalLogger.logVerbose("instance start at: ", Int(bitPattern: rawPointer))

// process dictionary

let _dict = convertKeyIfNeeded(dict: dict)

let instanceIsNsObject = instance.isNSObjectType()

let bridgedPropertyList = instance.getBridgedPropertyList()

for property in properties {

let isBridgedProperty = instanceIsNsObject && bridgedPropertyList.contains(property.key)

let propAddr = rawPointer.advanced(by: property.offset)

InternalLogger.logVerbose(property.key, "address at: ", Int(bitPattern: propAddr))

if mapper.propertyExcluded(key: Int(bitPattern: propAddr)) {

InternalLogger.logDebug("Exclude property: \(property.key)")

continue

}

let propertyDetail = PropertyInfo(key: property.key, type: property.type, address: propAddr, bridged: isBridgedProperty)

InternalLogger.logVerbose("field: ", property.key, "  offset: ", property.offset, "  isBridgeProperty: ", isBridgedProperty)

if let rawValue = getRawValueFrom(dict: _dict, property: propertyDetail, mapper: mapper) {

if let convertedValue = convertValue(rawValue: rawValue, property: propertyDetail, mapper: mapper) {

assignProperty(convertedValue: convertedValue, instance: instance, property: propertyDetail)

continue

}

}

InternalLogger.logDebug("Property: \(property.key) hasn't been written in")

}

}

HandyJSON代码阅读的更多相关文章

  1. 代码阅读分析工具Understand 2.0试用

    Understand 2.0是一款源代码阅读分析软件,功能强大.试用过一段时间后,感觉相当不错,确实可以大大提高代码阅读效率.由于Understand功能十分强大,本文不可能详尽地介绍它的所有功能,所 ...

  2. Android 上的代码阅读器 CoderBrowserHD 修改支持 go 语言代码

    我在Android上的代码阅读器用的是 https://github.com/zerob13/CoderBrowserHD 改造的版本,改造后的版本我放在 https://github.com/ghj ...

  3. Linux协议栈代码阅读笔记(二)网络接口的配置

    Linux协议栈代码阅读笔记(二)网络接口的配置 (基于linux-2.6.11) (一)用户态通过C库函数ioctl进行网络接口的配置 例如,知名的ifconfig程序,就是通过C库函数sys_io ...

  4. [置顶] Linux协议栈代码阅读笔记(一)

    Linux协议栈代码阅读笔记(一) (基于linux-2.6.21.7) (一)用户态通过诸如下面的C库函数访问协议栈服务 int socket(int domain, int type, int p ...

  5. 图形化代码阅读工具——Scitools Understand

    Scitools出品的Understand 2.0.用了很多年了,比Source Insight强大很多.以前的名字叫Understand for C/C++,Understand for Java, ...

  6. Python - 关于代码阅读的一些建议

    初始能力 让阅读思路保持清晰连贯,主力关注在流程架构和逻辑实现上,不被语法.技巧和业务流程等频繁地阻碍和打断. 建议基本满足以下条件,再开始进行代码阅读: 具备一定的语言基础:熟悉基础语法,常用的函数 ...

  7. MediaInfo代码阅读

      MediaInfo是一个用来分析媒体文件的开源工具. 支持的文件非常全面,基本上支持所有的媒体文件. 最近是在做HEVC开发,所以比较关注MediaInfo中关于HEVC的分析与处理. 从Meid ...

  8. Tools - 一些代码阅读的方法

    1 初始能力 让阅读思路清晰连贯,保持在程序的流程架构和逻辑实现上,不被语法.编程技巧和业务流程等频繁地阻碍和打断. 语言基础:熟悉基础语法,常用的函数.库.编程技巧等: 了解设计模式.构建工具.代码 ...

  9. Bleve代码阅读(二)——Index Mapping

    引言 Bleve是Golang实现的一个全文检索库,类似Lucene之于Java.在这里通过阅读其代码,来学习如何使用及定制检索功能.也是为了通过阅读代码,学习在具体环境下Golang的一些使用方式. ...

随机推荐

  1. WINDOWS安装mysql5.7.20

    MSI安装包链接 http://pan.baidu.com/s/1mhI0SMO 提取密码 gaqu 安装前要把老版本的MYSQL卸载干净 之前用官网的archive免安装版安装一直失败,放弃,用MS ...

  2. PyQt5 应用在 TeamViewer 下无法使用全屏模式

    PyQt5 应用在 TeamViewer 下无法使用全屏模式 问题描述 使用 PyQt5 (版本为 5.7)中的 QtWebEngineView 构建的桌面应用,部署到远程机器(Windows 7 平 ...

  3. sublime快捷键大全(转)

    文件 File 新建文件 Ctrl + N 打开文件 Ctrl + O 打开最近关闭的文件 Ctrl + Shift + T 保存 Ctrl + S 另存为… Ctrl + Shift + S 关闭文 ...

  4. Jquery+Aajax 批量上传

    注:转载请标明文章原始出处及作者信息 网上有现成的Uploadify.WebUpload等插件,自己写一个简单的(非插件). 1.页面 批量上传页面 <form action="&qu ...

  5. jquery+springMVC实现文件上传

    此文章是基于 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台 一. jar包介绍 1. commons-fileupload-1.3.1.jar 二. 相关程序 ...

  6. flask_restful的使用方法

    一 安装: pip install flask_restrul 二 初始化并注册路由 # run.py from flask_restful import Api ... api = Api(app) ...

  7. SQL SERVER 2012修改数据库名称(包括 db.mdf 名称的修改)

    假设原来数据库名为db,附加数据库为db.mdf和db_log.ldf.需要改成dbt,及dbt.mdf和dbt_log.ldf. 步骤: .首先把原来的数据库进行备份(选择数据库->右键-&g ...

  8. unity3d之技能栏冷却

    绑定在按钮上的脚本 using UnityEngine; using System.Collections; using UnityEngine.UI; public class CdCover : ...

  9. css3打包后自动追加前缀插件:autoprefixer

    用vue-cli构建的项目脚手架已经帮你把autoprefixer的配置做好了,自己不需要做什么改动就会自动加前缀: 下面一起看看涉及到autoprefixer这个插件的一些配置: 1,postcss ...

  10. 深入理解jQuery插件开发总结(四)

    jQuery插件开发模式 软件开发过程中是需要一定的设计模式来指导开发的,有了模式,我们就能更好地组织我们的代码,并且从这些前人总结出来的模式中学到很多好的实践. 根据<jQuery高级编程&g ...