Using DSLs to check function args.
including 5 functionalities:
- set default value if arg is nil.
- auto convert arg value, if arg value is compatible, like: "1" to 1.
- check whether arg is lacked
- check whether arg's type is error
- check whether arg's length/value is out of range
### Usage 1:
- configs keyword is necessary
- :string declare arg's data type.
- :optional declare arg's value that can be nil.
- :auto declare that lib convert it to integer value automatically if it is compatible.
- 10..20 means arg value should be in 10 to 20, not equal 10 or not equal 20.
- || operator is used to define default value, it also be a string/number/function.
with_check configs(
name(:string, :optional) || Module.A.B.get_ts(),
number(:integer, :auto, 10..20) || 10
) do
def create(name, number) do
:ok
end
end### Usage 2 :
defconfig also is a DSL that define the checking rule, including 2 parts:
- config name, must be an atom
- config rule, like: field_name(:string, :optional, :auto, 1..10) || 1
you can reuse config rule by config name.
defconfig(NameRule, name(:string))
with_check configs(NameRule, number(:integer)) do
def create(name, number) do
:ok
end
end### All DSLs
- defconfig
- with_check
### All Checking Data Type Values
- :string
- :integer
- :float
- :list
- :map
### Available Range format
- 1..10 (1 to 10, not equal 1 or not equal 10)
- 10 (10 to 10, equal to 10)
you can apply range setting to all data types.
### Available Default value format
- value, like: 1, 1.1, "default"
- function, like: Module.A.B.get_ts() or get_ts()
not support fn function at present.
### Functionalities
- :optional
- :auto, including 3 situations: (1). string to integer (2). string to float (3). integer to float