简体   繁体   English

在 Endpoint 中使用 oatpp::Enum 作为路径参数类型的正确方法是什么?

[英]What is the correct way to use oatpp::Enum as a path parameter type in Endpoint?

I'm using Oat++ framework ( https://oatpp.io/ ) to build a REST API.我正在使用 Oat++ 框架 ( https://oatpp.io/ ) 来构建 REST API。 I want to manage user roles, and I want to have an endpoint to update a single field of a user record - the user role.我想管理用户角色,并且我想要一个端点来更新用户记录的单个字段 - 用户角色。 To achieve this, I've created an end-point:为了实现这一点,我创建了一个端点:

ENDPOINT("PUT", "/users/{userId}/role/{role}", updateUserRole,
         PATH(oatpp::String, userId),
         PATH(oatpp::String, role))
{
  // TODO - Set user role here.
  return createResponse(Status::CODE_200, "OK");
}

Now, I want to validate that the user role has a correct value.现在,我想验证用户角色是否具有正确的值。 Specifically, that its value belongs to the set of allowed values.具体来说,它的值属于允许值的集合。 I could create some sort of a static set with predefined values and check if it contains the received value, but I wonder if there is a better way to this.我可以创建某种具有预定义值的静态集并检查它是否包含接收到的值,但我想知道是否有更好的方法。

I've found that Oatpp has some oatpp::Enum type but I can't find any examples using enum in the endpoint as a path parameter.我发现 Oatpp 有一些oatpp::Enum类型,但我找不到在端点中使用枚举作为路径参数的任何示例。

So, is it possible to use oatpp::Enum as a path parameter?那么,是否可以使用 oatpp::Enum 作为路径参数? If yes, then what is the correct way of using it?如果是,那么正确的使用方法是什么?

Thanks in advance!提前致谢!

Declare an Enum in the DTO code-gen section:在 DTO 代码生成部分声明一个Enum

ENUM(UserRoles, v_int32,
  VALUE(ADMIN, 1, "admin"), //<- you may use qualifiers here
  VALUE(GUEST, 2),
  VALUE(USER, 3)
)

Declare an endpoint:声明一个端点:

ENDPOINT("PUT", "/users/{userId}/role/{role}", updateUserRole,
         PATH(oatpp::String, userId),
         PATH(oatpp::Enum<UserRoles>, role))
{
  // TODO - Set user role here.
  return createResponse(Status::CODE_200, "OK");
}

oatpp::Enum will do all validations for you. oatpp::Enum会为你做所有的验证。

Valid request:有效请求:

$ curl -X PUT http://localhost:8000/users/user-1/role/GUEST
OK

Invalid request:无效请求:

$ curl -X PUT http://localhost:8000/users/user-1/role/SOMETHING
server=oatpp/1.1.0
code=400
description=Bad Request
message=Invalid PATH parameter 'role'. Expected type is 'oatpp::Enum<UserRoles>'

You can also use oatpp::Enum<UserRoles>::AsNumber if you want to receive integer values.如果您想接收整数值,您还可以使用oatpp::Enum<UserRoles>::AsNumber

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 此参数的正确类型是什么? - What is the correct type for this parameter? 在C ++中将字符串用作参数和参数或返回类型的正确方法是什么 - What is correct way to use string as an argument and parameter or return type in C++ 从基本类型(例如short)到与_mm256_broadcast_epi(例如_mm_broadcastw_epi16)一起使用,填充__m128i 参数的正确方法是什么? - What is the correct way to fill a __m128i parameter, from basic type (such as short), to use with _mm256_broadcast_epi (such as _mm_broadcastw_epi16) 我可以使用通用枚举类型作为 function 的参数吗? - Can I use a generic enum type as a parameter of a function? 在不使用指针的情况下,在C ++中返回“无效值”类型的正确方法是什么? - What is the correct way to return an 'Invalid Value' type in C++, without the use of pointers? 使用 decltype 作为尾随返回类型的正确方法 - Correct way to use decltype as trailing return type 在线程上使用MPI的正确方法是什么 - What is the correct way to use MPI with thread 使用GetLastError()的正确方法(时间)是什么? - What is the REALLY correct way (time) to use GetLastError()? 使用 fmt::sprintf 的正确方法是什么? - What is the correct way to use fmt::sprintf? 在 Crypto++ 中使用 ECDSA 的正确方法是什么 - What is the correct way to use ECDSA in Crypto++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM