Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 210 additions & 29 deletions doc/acid/typeutil.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
e!-- START doctoc generated TOC please keep comment here to allow nuto update -->
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
# Table of Content

Expand All @@ -7,15 +7,25 @@ e!-- START doctoc generated TOC please keep comment here to allow nuto update --
- [Description](#description)
- [Synopsis](#synopsis)
- [Methods](#methods)
- [typeutil.check_int_range](#typeutilcheck_int_range)
- [typeutil.check_integer_range](#typeutilcheck_integer_range)
- [typeutil.check_number_range](#typeutilcheck_number_range)
- [typeutil.check_number_string_range](#typeutilcheck_number_range)
- [typeutil.is_int](#typeutilis_int)
- [typeutil.check_string_number_range](#typeutilcheck_string_number_range)
- [typeutil.check_length_range](#typeutilcheck_length_range)
- [typeutil.check_fixed_length](#typeutilcheck_fixed_length)
- [typeutil.is_string](#typeutilis_string)
- [typeutil.is_number](#typeutilis_number)
- [typeutil.is_integer](#typeutilis_integer)
- [typeutil.is_boolean](#typeutilis_boolean)
- [typeutil.is_string_number](#typeutilis_string_number)
- [typeutil.is_array](#typeutilis_array)
- [typeutil.is_dict](#typeutilis_dict)
- [typeutil.is_empty_table](#typeutilis_empty_table)
- [Author](#author)
- [Copyright and License](#copyright-and-license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->


# Name

acid.typeutil
Expand All @@ -36,36 +46,43 @@ local acid_typeutil = require("acid.typeutil")
local is_true = acid_typeutil.check_number_range(1, 0, 2)
print(is_true) -- true

local is_true = acid_typeutil.is_int(0.5)
local is_true = acid_typeutil.is_integer(0.5)
print(is_true) -- false
```

# Methods


## typeutil.check_int_range
## typeutil.check_integer_range

**syntax**:
`typeutil.check_int_range(val, min, max)`
`typeutil.check_integer_range(val, min, max, opts)`

Return `true` if `val` is an integer and in the range,
`false` otherwise.

The range is closed.
It means `val` can be `min` or `max`.
Unlike `check_number_range()` in which a user is able to specify whether
left/right boundary is closed or opened.

**arguments**:

- `val`:
is a number.
is a number.

- `min`:
is the minimum of the range.
is the minimum of the range.

- `max`:
is the maximum of the range.
is the maximum of the range.

- `opts`:
`opts.left_closed` can be true or false.
Default is true.
If `opts.left_closed` is true, range contains min value.

`opts.right_closed` can be true or false.
Default is true.
If `opts.right_closed` is true, range contains max value.

**return**:
bool
Expand All @@ -74,57 +91,221 @@ bool
## typeutil.check_number_range

**syntax**:
`typeutil.check_number_range(val, min, max, left_closed, right_closed)`
`typeutil.check_number_range(val, min, max, opts)`

Return `true` if `val` is in the range,
`false` otherwise.

**arguments**:

- `val`:
is a number.
is a number.

- `min`:
is the minimum of the range.
is the minimum of the range.

- `max`:
is the maximum of the range.
is the maximum of the range.

- `opts`:
`opts.left_closed` can be true or false.
Default is true.
If `opts.left_closed` is true, range contains min value.

`opts.right_closed` can be true or false.
Default is true.
If `opts.right_closed` is true, range contains max value.

**return**:
bool.


## typeutil.check_string_number_range

**syntax**:
`typeutil.check_string_number_range(val, min, max, opts)`

Same as `typeutil.check_number_range` except the `val` is a string number.

## typeutil.check_length_range

**syntax**:
`typeutil.check_length_range(val, min, max, opts)`

Return `true` if `val`'s length is in the range,
`false` otherwise.

**arguments**:

- `val`:
can be a string or a table.

- `min`:
is the minimum of the range.

- `max`:
is the maximum of the range.

- `opts`:
`opts.left_closed` can be true or false.
Default is true.
If `opts.left_closed` is true, range contains min value.

`opts.right_closed` can be true or false.
Default is true.
If `opts.right_closed` is true, range contains max value.

**return**:
bool.

## typeutil.check_fixed_length

**syntax**:
`typeutil.check_fixed_length(val, length)`

- `left_closed`:
specifies the open or closed on the left of the range.
Return `true` if `val`'s length equals `length`,
`false` otherwise.

By default it is `true`.
**arguments**:

- `right_closed`:
specifies the open or closed on the right of the range.
- `val`:
can be a string or a table.

By default it is `true`.
- `length`:
is a number.

**return**:
bool.


## typeutil.check_number_string_range
## typeutil.is_string

**syntax**:
`typeutil.check_number_string_range(val, min, max, left_closed, right_closed)`
`typeutil.is_string(val)`

Same as `typeutil.check_number_range` except the `val` is a number or a number
string.
Return `true` if `val` is a string,
`false` otherwise.

**arguments**:

- `val`:
any.

**return**;
bool.

## typeutil.is_int
## typeutil.is_number

**syntax**:
`typeutil.is_int(val)`
`typeutil.is_number(val)`

Return `true` if `val` is a number,
`false` otherwise.

**arguments**:

- `val`:
any.

**return**;
bool.

## typeutil.is_integer

**syntax**:
`typeutil.is_integer(val)`

Return `true` if `val` is an integer,
`false` otherwise.

**arguments**:

- `val`:
is a number.
any.

**return**;
bool.

## typeutil.is_boolean

**syntax**:
`typeutil.is_boolean(val)`

Return `true` if `val` is a bool,
`false` otherwise.

**arguments**:

- `val`:
any.

**return**;
bool.

## typeutil.is_string_number

**syntax**:
`typeutil.is_string_number(val)`

Return `true` if `val` is a string number,
`false` otherwise.

**arguments**:

- `val`:
any.

**return**;
bool.

## typeutil.is_array

**syntax**:
`typeutil.is_array(val)`

Return `true` if `val` is an array,
`false` otherwise.

Note that the array follows the cjson definition.
See [encode_sparse_array](https://www.kyne.com.au/~mark/software/lua-cjson-manual.html#encode_sparse_array)
The excessively sparse array is considered to be a dict.
The empty_table is both an array and a dict.
**arguments**:

- `val`:
any.

**return**;
bool.

## typeutil.is_dict

**syntax**:
`typeutil.is_dict(val)`

Return `true` if `val` is a dict,
`false` otherwise.

**arguments**:

- `val`:
any.

**return**;
bool.

## typeutil.is_empty_table

**syntax**:
`typeutil.is_empty_table(val)`

Return `true` if `val` is `{}`,
`false` otherwise.

**arguments**:

- `val`:
any.

**return**;
bool.
Expand Down
8 changes: 4 additions & 4 deletions lib/acid/guid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ function _M.new(shared_guid, shared_lock, len_ts, len_seq, len_mid)
'shared dict is not exists: %s,%s', shared_guid, shared_lock)
end

if not typeutil.check_int_range(len_ts, 1, max_len_ts)
or not typeutil.check_int_range(len_seq, 1)
or not typeutil.check_int_range(len_mid, 1) then
if not typeutil.check_integer_range(len_ts, 1, max_len_ts)
or not typeutil.check_integer_range(len_seq, 1)
or not typeutil.check_integer_range(len_mid, 1) then

return nil, 'InvalidLength', string.format(
'lengths of guid parts are not integer or beyond range, %s, %s, %s',
Expand Down Expand Up @@ -82,7 +82,7 @@ end
function _M.generate(self, mid, max_wait_ms)
max_wait_ms = max_wait_ms or 500

if not typeutil.check_int_range(mid, 0, self.max_mid) then
if not typeutil.check_integer_range(mid, 0, self.max_mid) then
return nil, 'InvalidMachineId', string.format(
'mid %s is beyond range [0, %d]', tostring(mid), self.max_mid)
end
Expand Down
Loading