简体   繁体   English

使用flask-restx将正文中的json字段转换为枚举

[英]Convert json field in body to enum with flask-restx

I have the following api definition for flask-restx (though should also work with flask-restplus).我有以下flask-restx的api定义(尽管也应该与flask-restplus一起使用)。 Is there someway to convert the enum -field in the request body the the Enum MyEnum without too much overhead or using DAOs?有没有办法在没有太多开销或使用 DAO 的情况下将请求正文中的enum字段转换为 Enum MyEnum

class MyEnum(Enum):
    FOO = auto()
    BAR = auto()


@dataclass(frozen=True)
class MyClass:
    enum: MyEnum


api = Namespace('ns')

model = api.model('Model', {
    'enum': fields.String(enum=[x.name for x in MyEnum]),
})

@api.route('/')
class MyClass(Resource):

    @api.expect(Model)
    def post(self) -> None:
        c = MyClass(**api.payload)
        print(type(c.enum))  # <class 'str'> (but I want <enum 'MyEnum'>)
        assert(type(c.enum) == MyEnum)  # Fails

Ok I have written a decorator which will replace the enum value with the enum好的,我已经编写了一个装饰器,它将用枚举替换枚举值

def decode_enum(api: Namespace, enum_cls: Type[Enum], keys: List[str]):
    def replace_item(obj: dict, keys_: List[str], new_value: Type[Enum]):
        if not keys_:
            return new_value
        obj[keys_[0]] = replace_item(obj[keys_[0]], keys_[1:], new_value)
        return obj

    def decoder(f):
        @wraps(f)
        def wrapper(*args, **kwds):
            value = api.payload
            for k in keys:
                value = value[k]
            enum = enum_cls[value]
            api.payload[keys[0]] = replace_item(api.payload[keys[0]], keys[1:], enum)
            return f(*args, **kwds)
        return wrapper
    return decoder

The usage would be like this用法是这样的

@decode_enum(api, MyEnum, ['enum'])
@api.expect(Model)
def post(self) -> None:
    c = MyClass(**api.payload)
    print(type(c.enum))  # <enum 'MyEnum'>

The replace_item function was inspired by this SO Answer: https://stackoverflow.com/a/45335542/6900162 replace_item函数的灵感来自这个 SO 答案: https : //stackoverflow.com/a/45335542/6900162

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM