From 435979e76006a545b49a03459c49fa611a995609 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Fri, 5 Aug 2022 13:09:09 +0800 Subject: [PATCH] feat: self filter object --- Basis/Filter.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++ Filter/__init__.py | 8 +++-- 2 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 Basis/Filter.py diff --git a/Basis/Filter.py b/Basis/Filter.py new file mode 100644 index 0000000..7384495 --- /dev/null +++ b/Basis/Filter.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +filterObject = { + 'optional': { + 'optional': True, # `optional` is not force require + 'default': False, # disable `optional` option in default + 'allowNone': False, # `optional` couldn't be None + 'type': bool, + 'format': lambda x: x, # return same value + 'filter': lambda b: True, # always return True + 'errMsg': 'Invalid `optional` key' + }, + 'default': { + 'optional': True, # `default` is not force require + 'default': None, + 'allowNone': True, # `default` can be None + 'type': any, # skip type check + 'format': lambda x: x, # return same value + 'filter': lambda b: True, # always return True + 'errMsg': 'Invalid `default` key' + }, + 'allowNone': { + 'optional': True, # `allowNone` is not force require + 'default': False, # disable `allowNone` option in default + 'allowNone': False, # `allowNone` couldn't be None + 'type': bool, + 'format': lambda x: x, # return same value + 'filter': lambda b: True, # always return True + 'errMsg': 'Invalid `allowNone` key' + }, + 'type': { + 'optional': False, # `type` is force require + 'allowNone': False, # `type` couldn't be None + 'type': [any, type, list, dict], + 'format': lambda x: x, # return same value + 'filter': lambda b: True, # always return True + 'errMsg': 'Invalid `type` key' + }, + 'multiSub': { + 'optional': True, # `multiSub` is not force require + 'default': False, # disable `multiSub` option in default + 'allowNone': False, # `multiSub` couldn't be None + 'type': bool, + 'format': lambda x: x, # return same value + 'filter': lambda b: True, # always return True + 'errMsg': 'Invalid `multiSub` key' + }, + 'indexKey': { + 'optional': True, # `indexKey` is not force require + 'default': 'type', + 'allowNone': False, # `indexKey` couldn't be None + 'type': str, + 'format': lambda x: x, # return same value + 'filter': lambda b: True, # always return True + 'errMsg': 'Invalid `indexKey` key' + }, + 'format': { + 'optional': True, # `format` is not force require + 'default': lambda x: x, # don't change anything + 'allowNone': False, # `format` couldn't be None + 'type': any, + 'format': lambda x: x, # return same value + 'filter': lambda b: True, # always return True + 'errMsg': 'Invalid `format` key' + }, + 'filter': { + 'optional': True, # `filter` is not force require + 'default': lambda x: True, # always pass filter + 'allowNone': False, # `filter` couldn't be None + 'type': any, + 'format': lambda x: x, # return same value + 'filter': lambda b: True, # always return True + 'errMsg': 'Invalid `filter` key' + }, + 'errMsg': { + 'optional': True, # `errMsg` is not force require + 'default': 'filter error', + 'allowNone': False, # `errMsg` couldn't be None + 'type': str, + 'format': lambda x: x, # return same value + 'filter': lambda b: True, # always return True + 'errMsg': 'Invalid `errMsg` key' + }, +} diff --git a/Filter/__init__.py b/Filter/__init__.py index 50635e4..e0137b1 100644 --- a/Filter/__init__.py +++ b/Filter/__init__.py @@ -7,7 +7,7 @@ xxObject = { # a dict that describe multi-field 'optional': ..., # field force require or not 'default': ..., # default value when field is not exist (optional == True) 'allowNone': ..., # whether the value can be None (override the format and filter process) - 'type': ..., # type of field content (in filter process) (python type / dict) + 'type': ..., # type of field content (in filter process) (any / type / list / dict) 'multiSub': ..., # whether there are multi subObject (type is dict) 'indexKey': ..., # index key of subObject (type is dict and multiSub == True) 'format': ..., # format function (before filter process) (invalid content -> throw error) @@ -46,8 +46,10 @@ pre process format process -> set as field value (maybe throw error -> catch and throw errMsg) filter process - => type is `python type` -> compare with field type -> filter function check - => type is dict + => type is `any` -> filter function check (skip type compare) + => type is `type` -> compare with field type -> filter function check + => type is `list` -> compare every type in list with field -> filter function check + => type is `dict` => multiSub == False -> recursive check => multiSub == True => field content is not dict or not include indexKey -> throw error