From fd23b9bb8cade94731f541ca12e642bfdf5cd369 Mon Sep 17 00:00:00 2001 From: Oleg Jukovec Date: Thu, 20 Apr 2023 22:49:45 +0300 Subject: [PATCH] api: add Iterator Relates to https://github.com/tarantool/go-tarantool/pull/251 --- generate.go | 1 + generate.sh | 21 +++++++++++++++++++++ iterator.go | 23 +++++++++++++++++++++++ iterator_string.go | 34 ++++++++++++++++++++++++++++++++++ iterator_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+) create mode 100644 iterator.go create mode 100644 iterator_string.go create mode 100644 iterator_test.go diff --git a/generate.go b/generate.go index 6afb0a9..85c0089 100644 --- a/generate.go +++ b/generate.go @@ -4,6 +4,7 @@ package iproto //go:generate stringer -type=Error //go:generate stringer -type=Feature //go:generate stringer -type=Flag +//go:generate stringer -type=Iterator //go:generate stringer -type=Type //go:generate stringer -type=Key,MetadataKey,BallotKey,RaftKey,SqlInfoKey -output=keys_string.go //go:generate goimports -w . diff --git a/generate.sh b/generate.sh index a64ef29..061b3f6 100755 --- a/generate.sh +++ b/generate.sh @@ -25,6 +25,7 @@ SRC_CONST=${TT_DIR}/src/box/iproto_constants.h SRC_ERRORS=${TT_DIR}/src/box/errcode.h SRC_EXECUTE=${TT_DIR}/src/box/execute.h SRC_FEATURES=${TT_DIR}/src/box/iproto_features.h +SRC_ITERATORS=${TT_DIR}/src/box/iterator_type.h DST_DOC=doc.go DST_ERRORS=error.go DST_ERRORS_TEST=error_test.go @@ -32,6 +33,8 @@ DST_FEATURES=feature.go DST_FEATURES_TEST=feature_test.go DST_FLAGS=flag.go DST_FLAGS_TEST=flag_test.go +DST_ITERATORS=iterator.go +DST_ITERATORS_TEST=iterator_test.go DST_TYPES=type.go DST_TYPES_TEST=type_test.go DST_KEYS=keys.go @@ -236,6 +239,24 @@ echo "${FOOTER_TEST}" > ${DST_FLAGS_TEST} grep -PB 1 "[\t ]*IPROTO_FLAG_[A-Z_]+ =" ${SRC_CONST} | \ generate_test Flag >> ${DST_FLAGS_TEST} +# +# Iterators. +# + +echo "${FOOTER}" > ${DST_ITERATORS} +cat << EOF >> ${DST_ITERATORS} +// IPROTO iterators constants, generated from +// ${SRC_ITERATORS} +EOF + +read_enum iterator_type ${SRC_ITERATORS} | \ + generate_enum Iterator >> ${DST_ITERATORS} + +echo "${FOOTER_TEST}" > ${DST_ITERATORS_TEST} + +read_enum iterator_type ${SRC_ITERATORS} | \ + generate_test Iterator >> ${DST_ITERATORS_TEST} + # # Types. # diff --git a/iterator.go b/iterator.go new file mode 100644 index 0000000..40251e3 --- /dev/null +++ b/iterator.go @@ -0,0 +1,23 @@ +// Code generated by generate.sh; DO NOT EDIT. + +package iproto + +// IPROTO iterators constants, generated from +// tarantool/src/box/iterator_type.h +type Iterator int + +const ( + // ITER_EQ must be the first member for request_create + ITER_EQ Iterator = 0 // key == x ASC order + ITER_REQ Iterator = 1 // key == x DESC order + ITER_ALL Iterator = 2 // all tuples + ITER_LT Iterator = 3 // key < x + ITER_LE Iterator = 4 // key <= x + ITER_GE Iterator = 5 // key >= x + ITER_GT Iterator = 6 // key > x + ITER_BITS_ALL_SET Iterator = 7 // all bits from x are set in key + ITER_BITS_ANY_SET Iterator = 8 // at least one x's bit is set + ITER_BITS_ALL_NOT_SET Iterator = 9 // all bits are not set + ITER_OVERLAPS Iterator = 10 // key overlaps x + ITER_NEIGHBOR Iterator = 11 // tuples in distance ascending order from specified point +) diff --git a/iterator_string.go b/iterator_string.go new file mode 100644 index 0000000..c9c4e6d --- /dev/null +++ b/iterator_string.go @@ -0,0 +1,34 @@ +// Code generated by "stringer -type=Iterator"; DO NOT EDIT. + +package iproto + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[ITER_EQ-0] + _ = x[ITER_REQ-1] + _ = x[ITER_ALL-2] + _ = x[ITER_LT-3] + _ = x[ITER_LE-4] + _ = x[ITER_GE-5] + _ = x[ITER_GT-6] + _ = x[ITER_BITS_ALL_SET-7] + _ = x[ITER_BITS_ANY_SET-8] + _ = x[ITER_BITS_ALL_NOT_SET-9] + _ = x[ITER_OVERLAPS-10] + _ = x[ITER_NEIGHBOR-11] +} + +const _Iterator_name = "ITER_EQITER_REQITER_ALLITER_LTITER_LEITER_GEITER_GTITER_BITS_ALL_SETITER_BITS_ANY_SETITER_BITS_ALL_NOT_SETITER_OVERLAPSITER_NEIGHBOR" + +var _Iterator_index = [...]uint8{0, 7, 15, 23, 30, 37, 44, 51, 68, 85, 106, 119, 132} + +func (i Iterator) String() string { + if i < 0 || i >= Iterator(len(_Iterator_index)-1) { + return "Iterator(" + strconv.FormatInt(int64(i), 10) + ")" + } + return _Iterator_name[_Iterator_index[i]:_Iterator_index[i+1]] +} diff --git a/iterator_test.go b/iterator_test.go new file mode 100644 index 0000000..a98249c --- /dev/null +++ b/iterator_test.go @@ -0,0 +1,42 @@ +// Code generated by generate.sh; DO NOT EDIT. + +package iproto_test + +import ( + "testing" + + "github.com/tarantool/go-iproto" +) + +func TestIterator(t *testing.T) { + cases := []struct { + Iterator iproto.Iterator + Str string + Val int + }{ + + {iproto.ITER_EQ, "ITER_EQ", 0}, + {iproto.ITER_REQ, "ITER_REQ", 1}, + {iproto.ITER_ALL, "ITER_ALL", 2}, + {iproto.ITER_LT, "ITER_LT", 3}, + {iproto.ITER_LE, "ITER_LE", 4}, + {iproto.ITER_GE, "ITER_GE", 5}, + {iproto.ITER_GT, "ITER_GT", 6}, + {iproto.ITER_BITS_ALL_SET, "ITER_BITS_ALL_SET", 7}, + {iproto.ITER_BITS_ANY_SET, "ITER_BITS_ANY_SET", 8}, + {iproto.ITER_BITS_ALL_NOT_SET, "ITER_BITS_ALL_NOT_SET", 9}, + {iproto.ITER_OVERLAPS, "ITER_OVERLAPS", 10}, + {iproto.ITER_NEIGHBOR, "ITER_NEIGHBOR", 11}, + } + + for _, tc := range cases { + t.Run(tc.Str, func(t *testing.T) { + if tc.Iterator.String() != tc.Str { + t.Errorf("Got %s, expected %s", tc.Iterator.String(), tc.Str) + } + if int(tc.Iterator) != tc.Val { + t.Errorf("Got %d, expected %d", tc.Iterator, tc.Val) + } + }) + } +}