add more descriptions

This commit is contained in:
CaptainNEO 2022-03-10 21:14:17 +08:00
parent 3a78fd393d
commit 0e47fbc7f3

106
README.md
View File

@ -68,7 +68,6 @@ OpenAPI Specification下文简称OAS定义了一个标准的、语言无
以及下文中所涉及项目的完整代码均保存在本仓库中。 以及下文中所涉及项目的完整代码均保存在本仓库中。
### 1. Goa ### 1. Goa
#### 1. 开发过程 #### 1. 开发过程
@ -500,6 +499,55 @@ class StudentView(viewsets.ModelViewSet):
查看[main.py](./python/fastapi_example/main.py) 查看[main.py](./python/fastapi_example/main.py)
```python
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel, Field
app = FastAPI()
class Item(BaseModel):
id: Optional[int] = None
name: str = Field(description="The name of the item", regex="^[a-zA-Z0-9]*$")
price: float
is_offer: Optional[bool] = None
db = {
1: Item(id=1, name="Foo", price=19.99),
2: Item(id=2, name="Bar", price=29.99),
3: Item(id=3, name="Baz", price=39.99),
}
@app.get("/")
def get_items(
limit: int = 10, # 一个可选的int类型的参数名字是limit且默认值为10通过query string传递
offset: int = Field(..., description="db offset"), # 一个必选的int类型的参数名字是offset通过query string传递
q: Optional[str] = None, # 一个可选的str类型的参数名字是q通过query string传递
):
return [
{"item": db[k], "price": db[k].price}
for k in sorted(db.keys())[offset : offset + limit]
]
@app.get("/items/{item_id}")
def read_item(item_id: int):
return db[item_id]
@app.post("/items")
def create_item(item: Item) -> Item:
pid = list(db.keys())[-1] + 1
item.id = pid
db[pid] = item
return item
```
##### 2. 使用uvicorn启动服务 ##### 2. 使用uvicorn启动服务
```bash ```bash
@ -528,7 +576,59 @@ uvicorn main:app
#### 1. 开发过程 #### 1. 开发过程
这里采用官方用例提供的一个模拟用户CURD的例子 这里采用官方用例提供的一个模拟用户CURD的例子
查看代码[main.rs](./rust/poem/src/main.rs) 完整代码查看[main.rs](./rust/poem/src/main.rs)
```rust
#[derive(Tags)]
enum ApiTags {
/// Operations about user
User,
}
#[OpenApi]
impl Api {
/// Update user by id <<<<<这里的注释是三条斜线不是普通的注释程序编译的时候可以拿到这个信息
#[oai(path = "/users/:user_id", method = "put", tag = "ApiTags::User")]
async fn put_user(&self, user_id: Path<i64>, update: Json<UpdateUser>) -> UpdateUserResponse {
let mut users = self.users.lock().await;
match users.get_mut(user_id.0 as usize) {
Some(user) => {
if let Some(name) = update.0.name {
user.name = name;
}
if let Some(password) = update.0.password {
user.password = password;
}
UpdateUserResponse::Ok
}
None => UpdateUserResponse::NotFound,
}
}
}
/// Update user schema
#[derive(Debug, Object, Clone, Eq, PartialEq)]
struct UpdateUser {
/// Name
#[oai(validator(max_length = 20))]
name: Option<String>,
/// Password
password: Option<Password>,
}
#[derive(ApiResponse)]
enum UpdateUserResponse {
/// Returns when the user is successfully updated.
#[oai(status = 200)]
Ok,
/// Return when the specified user is not found.
#[oai(status = 404)]
NotFound,
}
```
#### 2. OAS的实现方式 #### 2. OAS的实现方式
@ -536,13 +636,11 @@ uvicorn main:app
2. 函数的入参和返回用于判断接口的Request和Response这里有点类似FastAPI只不过python用的类型注解来判断参数类型rust使用的是泛型。 2. 函数的入参和返回用于判断接口的Request和Response这里有点类似FastAPI只不过python用的类型注解来判断参数类型rust使用的是泛型。
3. 编译的时候,这些宏在全部展开,配合[Doc Comment](https://doc.rust-lang.org/rust-by-example/meta/doc.html)可以得到OpenAPI所需的全部信息。 3. 编译的时候,这些宏在全部展开,配合[Doc Comment](https://doc.rust-lang.org/rust-by-example/meta/doc.html)可以得到OpenAPI所需的全部信息。
#### 3. 优点 #### 3. 优点
1. Rust语言提供零成本抽象能力、零GC、保证内存安全等特性使其性能非常优异。而且语言为用户提供了非常丰富的抽象能力、和特性。 1. Rust语言提供零成本抽象能力、零GC、保证内存安全等特性使其性能非常优异。而且语言为用户提供了非常丰富的抽象能力、和特性。
2. 框架本身提供的宏非常简洁。Go1.18宏正式启用后,可以调研是否可以借鉴思想。 2. 框架本身提供的宏非常简洁。Go1.18宏正式启用后,可以调研是否可以借鉴思想。
#### 4. 缺点 #### 4. 缺点
1. Rust的学习成本比起Go语言要高市场上相关人才比较少。 1. Rust的学习成本比起Go语言要高市场上相关人才比较少。