Browse Source

feat: self filter object

master
dnomd343 2 years ago
parent
commit
435979e760
  1. 85
      Basis/Filter.py
  2. 8
      Filter/__init__.py

85
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'
},
}

8
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

Loading…
Cancel
Save