Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spec on not found #28

Open
lepture opened this issue Dec 15, 2015 · 6 comments
Open

spec on not found #28

lepture opened this issue Dec 15, 2015 · 6 comments

Comments

@lepture
Copy link
Member

lepture commented Dec 15, 2015

定义未找到 code 时返回什么?

@fahchen
Copy link
Member

fahchen commented Dec 17, 2015

Return prefecture level division of current division. If current division is a province, return none/null/nil.

https://github.com/cn/GB2260/blob/develop/spec.md#prefecture

@lepture
Copy link
Member Author

lepture commented Dec 17, 2015

@fahchen 不是这个,是 .get(code) 这样的 method 的返回值问题。

@fahchen
Copy link
Member

fahchen commented Dec 17, 2015

我的意思是,既然这个方法的未找到是这样定义的,那这里是不是也应该保持一致

@damonchen
Copy link
Contributor

我的实现是nil。从规范上看,这儿返回的是一个json的object,也就是其他语言的struct或者object,大部分是后,可以用空的形式来表示没有找到,我建议用none/nil/null 这样的来表示。

@gaomd
Copy link
Member

gaomd commented Dec 5, 2016

从 REST API 的设计角度考虑

  1. 如果某个资源未找到,则应该返回 404 状态,对应到 method 设计的话应该是返回值 NULL

  2. 而如果资源存在,但是资源中的某几个字段不存在,则对应字段不应省略,而是将值设为 NULL,对应到 method 设计的话应该是正常返回 struct / object,但是其中的字段没有值的话则为 NULL / ""(空字符串)。

    参考 GitHub API:Blank fields are included as null instead of being omitted.


针对 gb2260.get("110105") 这种方法,方案 1 应该是更合理的,代表资源未找到。

实现上就像 @damonchen 那样,如果找到该 code,则返回对应资源,反之返回 NULL

对于某些静态类型(?)语言可能需要费一些周折,对于这类语言,可以引入 Exception,当资源未找到时触发 Exception。

@tonyseek
Copy link
Contributor

tonyseek commented Dec 5, 2016

IMO 使用异常,还是 nil 值,还是 Optional 类型,看各语言的 idiomatic 用法即可… spec 也可以仅要求这一点

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants