Files
gf/container/garray/garray_normal_str.go

522 lines
15 KiB
Go
Raw Permalink Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package garray
import (
2019-06-19 09:06:52 +08:00
"bytes"
"sort"
"strings"
"sync"
2021-11-13 23:23:55 +08:00
"github.com/gogf/gf/v2/text/gstr"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/util/gconv"
)
2020-03-30 20:56:00 +08:00
// StrArray is a golang string array with rich features.
2020-05-22 12:04:58 +08:00
// It contains a concurrent-safe/unsafe switch, which should be set
// when its initialization and cannot be changed then.
2019-09-05 11:38:36 +08:00
type StrArray struct {
*TArray[string]
once sync.Once
}
2019-09-05 11:38:36 +08:00
// NewStrArray creates and returns an empty array.
2021-06-02 09:42:27 +08:00
// The parameter `safe` is used to specify whether using array in concurrent-safety,
2019-04-24 22:23:32 +08:00
// which is false in default.
2019-09-05 11:38:36 +08:00
func NewStrArray(safe ...bool) *StrArray {
return NewStrArraySize(0, 0, safe...)
}
2019-09-05 11:38:36 +08:00
// NewStrArraySize create and returns an array with given size and cap.
2021-06-02 09:42:27 +08:00
// The parameter `safe` is used to specify whether using array in concurrent-safety,
2019-04-24 22:23:32 +08:00
// which is false in default.
2019-09-05 11:38:36 +08:00
func NewStrArraySize(size int, cap int, safe ...bool) *StrArray {
return &StrArray{
TArray: NewTArraySize[string](size, cap, safe...),
2019-06-19 09:06:52 +08:00
}
2019-02-01 17:30:23 +08:00
}
2021-06-02 09:42:27 +08:00
// NewStrArrayFrom creates and returns an array with given slice `array`.
// The parameter `safe` is used to specify whether using array in concurrent-safety,
2019-04-24 22:23:32 +08:00
// which is false in default.
2019-09-05 11:38:36 +08:00
func NewStrArrayFrom(array []string, safe ...bool) *StrArray {
return &StrArray{
TArray: NewTArrayFrom(array, safe...),
}
}
2021-06-02 09:42:27 +08:00
// NewStrArrayFromCopy creates and returns an array from a copy of given slice `array`.
// The parameter `safe` is used to specify whether using array in concurrent-safety,
2019-04-24 22:23:32 +08:00
// which is false in default.
2019-09-05 11:38:36 +08:00
func NewStrArrayFromCopy(array []string, safe ...bool) *StrArray {
2019-06-19 09:06:52 +08:00
newArray := make([]string, len(array))
copy(newArray, array)
return NewStrArrayFrom(newArray, safe...)
}
// lazyInit lazily initializes the array.
func (a *StrArray) lazyInit() {
a.once.Do(func() {
if a.TArray == nil {
a.TArray = NewTArray[string](false)
}
})
}
2021-06-02 09:42:27 +08:00
// At returns the value by the specified index.
// If the given `index` is out of range of the array, it returns an empty string.
func (a *StrArray) At(index int) (value string) {
a.lazyInit()
return a.TArray.At(index)
2021-06-02 09:42:27 +08:00
}
2020-03-30 20:31:47 +08:00
// Get returns the value by the specified index.
2021-06-02 09:42:27 +08:00
// If the given `index` is out of range of the array, the `found` is false.
2020-03-30 20:31:47 +08:00
func (a *StrArray) Get(index int) (value string, found bool) {
a.lazyInit()
return a.TArray.Get(index)
}
2019-04-24 22:23:32 +08:00
// Set sets value to specified index.
2020-03-30 20:31:47 +08:00
func (a *StrArray) Set(index int, value string) error {
a.lazyInit()
return a.TArray.Set(index, value)
}
2021-06-02 09:42:27 +08:00
// SetArray sets the underlying slice array with the given `array`.
2019-09-05 11:38:36 +08:00
func (a *StrArray) SetArray(array []string) *StrArray {
a.lazyInit()
a.TArray.SetArray(array)
2019-06-19 09:06:52 +08:00
return a
}
2021-06-02 09:42:27 +08:00
// Replace replaces the array items by given `array` from the beginning of array.
2019-09-05 11:38:36 +08:00
func (a *StrArray) Replace(array []string) *StrArray {
a.lazyInit()
a.TArray.Replace(array)
2019-06-19 09:06:52 +08:00
return a
2019-02-01 17:30:23 +08:00
}
2019-04-24 22:23:32 +08:00
// Sum returns the sum of values in an array.
2019-09-05 11:38:36 +08:00
func (a *StrArray) Sum() (sum int) {
a.lazyInit()
return a.TArray.Sum()
2019-02-01 17:30:23 +08:00
}
2019-04-24 22:23:32 +08:00
// Sort sorts the array in increasing order.
2021-06-02 09:42:27 +08:00
// The parameter `reverse` controls whether sort
2019-02-02 14:22:32 +08:00
// in increasing order(default) or decreasing order
2019-09-05 11:38:36 +08:00
func (a *StrArray) Sort(reverse ...bool) *StrArray {
a.lazyInit()
2019-06-19 09:06:52 +08:00
a.mu.Lock()
defer a.mu.Unlock()
if len(reverse) > 0 && reverse[0] {
sort.Slice(a.array, func(i, j int) bool {
2021-07-28 22:18:06 +08:00
return strings.Compare(a.array[i], a.array[j]) >= 0
2019-06-19 09:06:52 +08:00
})
} else {
sort.Strings(a.array)
}
return a
}
2021-06-02 09:42:27 +08:00
// SortFunc sorts the array by custom function `less`.
2019-09-05 11:38:36 +08:00
func (a *StrArray) SortFunc(less func(v1, v2 string) bool) *StrArray {
a.lazyInit()
a.TArray.SortFunc(less)
2019-06-19 09:06:52 +08:00
return a
2019-02-01 18:33:53 +08:00
}
2023-02-08 19:08:10 +08:00
// InsertBefore inserts the `values` to the front of `index`.
func (a *StrArray) InsertBefore(index int, values ...string) error {
a.lazyInit()
return a.TArray.InsertBefore(index, values...)
}
2023-02-08 19:08:10 +08:00
// InsertAfter inserts the `values` to the back of `index`.
func (a *StrArray) InsertAfter(index int, values ...string) error {
a.lazyInit()
return a.TArray.InsertAfter(index, values...)
}
2019-04-24 22:23:32 +08:00
// Remove removes an item by index.
2021-06-02 09:42:27 +08:00
// If the given `index` is out of range of the array, the `found` is false.
2020-03-30 20:31:47 +08:00
func (a *StrArray) Remove(index int) (value string, found bool) {
a.lazyInit()
return a.TArray.Remove(index)
}
// RemoveValue removes an item by value.
// It returns true if value is found in the array, or else false if not found.
func (a *StrArray) RemoveValue(value string) bool {
a.lazyInit()
return a.TArray.RemoveValue(value)
}
// RemoveValues removes multiple items by `values`.
func (a *StrArray) RemoveValues(values ...string) {
a.lazyInit()
a.TArray.RemoveValues(values...)
}
2019-04-24 22:23:32 +08:00
// PushLeft pushes one or multiple items to the beginning of array.
2019-09-05 11:38:36 +08:00
func (a *StrArray) PushLeft(value ...string) *StrArray {
a.lazyInit()
a.TArray.PushLeft(value...)
2019-06-19 09:06:52 +08:00
return a
}
2019-04-24 22:23:32 +08:00
// PushRight pushes one or multiple items to the end of array.
// It equals to Append.
2019-09-05 11:38:36 +08:00
func (a *StrArray) PushRight(value ...string) *StrArray {
a.lazyInit()
a.TArray.PushRight(value...)
2019-06-19 09:06:52 +08:00
return a
}
2019-04-24 22:23:32 +08:00
// PopLeft pops and returns an item from the beginning of array.
2021-06-02 09:42:27 +08:00
// Note that if the array is empty, the `found` is false.
2020-03-30 20:31:47 +08:00
func (a *StrArray) PopLeft() (value string, found bool) {
a.lazyInit()
return a.TArray.PopLeft()
}
2019-04-24 22:23:32 +08:00
// PopRight pops and returns an item from the end of array.
2021-06-02 09:42:27 +08:00
// Note that if the array is empty, the `found` is false.
2020-03-30 20:31:47 +08:00
func (a *StrArray) PopRight() (value string, found bool) {
a.lazyInit()
return a.TArray.PopRight()
}
2019-04-24 22:23:32 +08:00
// PopRand randomly pops and return an item out of array.
2021-06-02 09:42:27 +08:00
// Note that if the array is empty, the `found` is false.
2020-03-30 20:31:47 +08:00
func (a *StrArray) PopRand() (value string, found bool) {
a.lazyInit()
return a.TArray.PopRand()
}
2021-06-02 09:42:27 +08:00
// PopRands randomly pops and returns `size` items out of array.
// If the given `size` is greater than size of the array, it returns all elements of the array.
// Note that if given `size` <= 0 or the array is empty, it returns nil.
2019-09-05 11:38:36 +08:00
func (a *StrArray) PopRands(size int) []string {
a.lazyInit()
return a.TArray.PopRands(size)
}
2021-06-02 09:42:27 +08:00
// PopLefts pops and returns `size` items from the beginning of array.
// If the given `size` is greater than size of the array, it returns all elements of the array.
// Note that if given `size` <= 0 or the array is empty, it returns nil.
2019-09-05 11:38:36 +08:00
func (a *StrArray) PopLefts(size int) []string {
a.lazyInit()
return a.TArray.PopLefts(size)
2019-02-02 14:22:32 +08:00
}
2021-06-02 09:42:27 +08:00
// PopRights pops and returns `size` items from the end of array.
// If the given `size` is greater than size of the array, it returns all elements of the array.
// Note that if given `size` <= 0 or the array is empty, it returns nil.
2019-09-05 11:38:36 +08:00
func (a *StrArray) PopRights(size int) []string {
a.lazyInit()
return a.TArray.PopRights(size)
2019-02-02 14:22:32 +08:00
}
2019-04-24 22:23:32 +08:00
// Range picks and returns items by range, like array[start:end].
// Notice, if in concurrent-safe usage, it returns a copy of slice;
2019-02-02 14:22:32 +08:00
// else a pointer to the underlying data.
//
2021-06-02 09:42:27 +08:00
// If `end` is negative, then the offset will start from the end of array.
// If `end` is omitted, then the sequence will have everything from start up
// until the end of the array.
2019-09-05 11:38:36 +08:00
func (a *StrArray) Range(start int, end ...int) []string {
a.lazyInit()
return a.TArray.Range(start, end...)
2019-02-02 14:22:32 +08:00
}
// SubSlice returns a slice of elements from the array as specified
2021-06-02 09:42:27 +08:00
// by the `offset` and `size` parameters.
// If in concurrent safe usage, it returns a copy of the slice; else a pointer.
//
// If offset is non-negative, the sequence will start at that offset in the array.
// If offset is negative, the sequence will start that far from the end of the array.
//
// If length is given and is positive, then the sequence will have up to that many elements in it.
// If the array is shorter than the length, then only the available array elements will be present.
// If length is given and is negative then the sequence will stop that many elements from the end of the array.
// If it is omitted, then the sequence will have everything from offset up until the end of the array.
//
// Any possibility crossing the left border of array, it will fail.
2019-09-05 11:38:36 +08:00
func (a *StrArray) SubSlice(offset int, length ...int) []string {
a.lazyInit()
return a.TArray.SubSlice(offset, length...)
}
2021-08-04 23:35:29 +08:00
// Append is alias of PushRight,please See PushRight.
2019-09-05 11:38:36 +08:00
func (a *StrArray) Append(value ...string) *StrArray {
a.lazyInit()
a.TArray.Append(value...)
2019-06-19 09:06:52 +08:00
return a
}
2019-04-24 22:23:32 +08:00
// Len returns the length of array.
2019-09-05 11:38:36 +08:00
func (a *StrArray) Len() int {
a.lazyInit()
return a.TArray.Len()
}
2019-04-24 22:23:32 +08:00
// Slice returns the underlying data of array.
// Note that, if it's in concurrent-safe usage, it returns a copy of underlying data,
// or else a pointer to the underlying data.
2019-09-05 11:38:36 +08:00
func (a *StrArray) Slice() []string {
a.lazyInit()
return a.TArray.Slice()
}
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395) This pull request standardizes the use of the Go 1.18+ `any` type alias instead of `interface{}` throughout the codebase. The change improves code readability and aligns with modern Go best practices. The update touches many files, including core data structures, code generation templates, logging utilities, and test data, ensuring consistency across all usages. **Type alias migration to `any`:** * Replaced all instances of `interface{}` with `any` in core data structures such as `garray` and in generated model structs (e.g., `TableUser`, `User1`, `User2`) to modernize type usage. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) * Updated function signatures, method parameters, and return types from `interface{}` to `any` in various parts of the codebase, including code generation, service logic, and logging utilities (e.g., `mlog`). [[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74) [[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) [[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107) [[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121) [[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112) [[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36) [[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74) [[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96) **Generated code and templates:** * Adjusted generated files and code generation templates to output `any` instead of `interface{}` for relevant struct fields and function signatures, ensuring that new code generation aligns with the updated convention. [[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) [[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) **Container and utility updates:** * Refactored the `garray` container implementation and related constructors/methods to use `[]any` instead of `[]interface{}`, along with corresponding function signatures. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52) [[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62) [[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86) [[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97) [[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114) [[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124) [[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143) [[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167) These changes collectively modernize the codebase and prepare it for future Go developments by using the idiomatic `any` type.
2025-08-28 16:53:19 +08:00
// Interfaces returns current array as []any.
func (a *StrArray) Interfaces() []any {
a.lazyInit()
return a.TArray.Interfaces()
2019-10-21 19:13:25 +08:00
}
2019-04-24 22:23:32 +08:00
// Clone returns a new array, which is a copy of current array.
2019-09-05 11:38:36 +08:00
func (a *StrArray) Clone() (newArray *StrArray) {
a.lazyInit()
return &StrArray{
TArray: a.TArray.Clone(),
}
}
2019-04-24 22:23:32 +08:00
// Clear deletes all items of current array.
2019-09-05 11:38:36 +08:00
func (a *StrArray) Clear() *StrArray {
a.lazyInit()
a.TArray.Clear()
2019-06-19 09:06:52 +08:00
return a
}
2019-04-24 22:23:32 +08:00
// Contains checks whether a value exists in the array.
2019-09-05 11:38:36 +08:00
func (a *StrArray) Contains(value string) bool {
a.lazyInit()
return a.TArray.Contains(value)
}
// ContainsI checks whether a value exists in the array with case-insensitively.
// Note that it internally iterates the whole array to do the comparison with case-insensitively.
func (a *StrArray) ContainsI(value string) bool {
a.lazyInit()
a.mu.RLock()
defer a.mu.RUnlock()
if len(a.array) == 0 {
return false
}
for _, v := range a.array {
if strings.EqualFold(v, value) {
return true
}
}
return false
}
2021-06-02 09:42:27 +08:00
// Search searches array by `value`, returns the index of `value`,
2019-04-24 22:23:32 +08:00
// or returns -1 if not exists.
2019-09-05 11:38:36 +08:00
func (a *StrArray) Search(value string) int {
a.lazyInit()
return a.TArray.Search(value)
}
2019-04-24 22:23:32 +08:00
// Unique uniques the array, clear repeated items.
2020-05-08 17:12:37 +08:00
// Example: [1,1,2,3,2] -> [1,2,3]
2019-09-05 11:38:36 +08:00
func (a *StrArray) Unique() *StrArray {
a.lazyInit()
a.TArray.Unique()
return a
}
2021-06-02 09:42:27 +08:00
// LockFunc locks writing by callback function `f`.
2019-09-05 11:38:36 +08:00
func (a *StrArray) LockFunc(f func(array []string)) *StrArray {
a.lazyInit()
a.TArray.LockFunc(f)
2019-06-19 09:06:52 +08:00
return a
}
2021-06-02 09:42:27 +08:00
// RLockFunc locks reading by callback function `f`.
2019-09-05 11:38:36 +08:00
func (a *StrArray) RLockFunc(f func(array []string)) *StrArray {
a.lazyInit()
a.TArray.RLockFunc(f)
2019-06-19 09:06:52 +08:00
return a
}
2021-06-02 09:42:27 +08:00
// Merge merges `array` into current array.
// The parameter `array` can be any garray or slice type.
// The difference between Merge and Append is Append supports only specified slice type,
2019-04-24 22:23:32 +08:00
// but Merge supports more parameter types.
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395) This pull request standardizes the use of the Go 1.18+ `any` type alias instead of `interface{}` throughout the codebase. The change improves code readability and aligns with modern Go best practices. The update touches many files, including core data structures, code generation templates, logging utilities, and test data, ensuring consistency across all usages. **Type alias migration to `any`:** * Replaced all instances of `interface{}` with `any` in core data structures such as `garray` and in generated model structs (e.g., `TableUser`, `User1`, `User2`) to modernize type usage. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) * Updated function signatures, method parameters, and return types from `interface{}` to `any` in various parts of the codebase, including code generation, service logic, and logging utilities (e.g., `mlog`). [[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74) [[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) [[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107) [[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121) [[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112) [[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36) [[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74) [[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96) **Generated code and templates:** * Adjusted generated files and code generation templates to output `any` instead of `interface{}` for relevant struct fields and function signatures, ensuring that new code generation aligns with the updated convention. [[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) [[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) **Container and utility updates:** * Refactored the `garray` container implementation and related constructors/methods to use `[]any` instead of `[]interface{}`, along with corresponding function signatures. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52) [[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62) [[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86) [[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97) [[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114) [[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124) [[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143) [[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167) These changes collectively modernize the codebase and prepare it for future Go developments by using the idiomatic `any` type.
2025-08-28 16:53:19 +08:00
func (a *StrArray) Merge(array any) *StrArray {
return a.Append(gconv.Strings(array)...)
}
2021-06-02 09:42:27 +08:00
// Fill fills an array with num entries of the value `value`,
// keys starting at the `startIndex` parameter.
2020-03-30 20:31:47 +08:00
func (a *StrArray) Fill(startIndex int, num int, value string) error {
a.lazyInit()
return a.TArray.Fill(startIndex, num, value)
}
2019-04-24 22:23:32 +08:00
// Chunk splits an array into multiple arrays,
2021-06-02 09:42:27 +08:00
// the size of each array is determined by `size`.
2019-02-02 14:22:32 +08:00
// The last chunk may contain less than size elements.
2019-09-05 11:38:36 +08:00
func (a *StrArray) Chunk(size int) [][]string {
a.lazyInit()
return a.TArray.Chunk(size)
}
2021-06-02 09:42:27 +08:00
// Pad pads array to the specified length with `value`.
2019-04-24 22:23:32 +08:00
// If size is positive then the array is padded on the right, or negative on the left.
2021-06-02 09:42:27 +08:00
// If the absolute value of `size` is less than or equal to the length of the array
// then no padding takes place.
2019-09-05 11:38:36 +08:00
func (a *StrArray) Pad(size int, value string) *StrArray {
a.lazyInit()
a.TArray.Pad(size, value)
2019-06-19 09:06:52 +08:00
return a
}
2019-04-24 22:23:32 +08:00
// Rand randomly returns one item from array(no deleting).
2020-03-30 20:31:47 +08:00
func (a *StrArray) Rand() (value string, found bool) {
a.lazyInit()
return a.TArray.Rand()
}
2021-06-02 09:42:27 +08:00
// Rands randomly returns `size` items from array(no deleting).
2019-09-05 11:38:36 +08:00
func (a *StrArray) Rands(size int) []string {
a.lazyInit()
return a.TArray.Rands(size)
}
2019-04-24 22:23:32 +08:00
// Shuffle randomly shuffles the array.
2019-09-05 11:38:36 +08:00
func (a *StrArray) Shuffle() *StrArray {
a.lazyInit()
a.TArray.Shuffle()
2019-06-19 09:06:52 +08:00
return a
}
2019-04-24 22:23:32 +08:00
// Reverse makes array with elements in reverse order.
2019-09-05 11:38:36 +08:00
func (a *StrArray) Reverse() *StrArray {
a.lazyInit()
a.TArray.Reverse()
2019-06-19 09:06:52 +08:00
return a
}
2021-06-02 09:42:27 +08:00
// Join joins array elements with a string `glue`.
2019-09-05 11:38:36 +08:00
func (a *StrArray) Join(glue string) string {
a.lazyInit()
return a.TArray.Join(glue)
}
2019-04-24 22:23:32 +08:00
// CountValues counts the number of occurrences of all values in the array.
2019-09-05 11:38:36 +08:00
func (a *StrArray) CountValues() map[string]int {
a.lazyInit()
return a.TArray.CountValues()
2019-04-24 22:23:32 +08:00
}
// Iterator is alias of IteratorAsc.
func (a *StrArray) Iterator(f func(k int, v string) bool) {
a.IteratorAsc(f)
}
2021-06-02 09:42:27 +08:00
// IteratorAsc iterates the array readonly in ascending order with given callback function `f`.
// If `f` returns true, then it continues iterating; or false to stop.
func (a *StrArray) IteratorAsc(f func(k int, v string) bool) {
a.lazyInit()
a.TArray.IteratorAsc(f)
}
2021-06-02 09:42:27 +08:00
// IteratorDesc iterates the array readonly in descending order with given callback function `f`.
// If `f` returns true, then it continues iterating; or false to stop.
func (a *StrArray) IteratorDesc(f func(k int, v string) bool) {
a.lazyInit()
a.TArray.IteratorDesc(f)
}
// String returns current array as a string, which implements like json.Marshal does.
2019-09-05 11:38:36 +08:00
func (a *StrArray) String() string {
2022-03-21 22:04:15 +08:00
if a == nil {
return ""
}
a.lazyInit()
a.mu.RLock()
defer a.mu.RUnlock()
buffer := bytes.NewBuffer(nil)
buffer.WriteByte('[')
for k, v := range a.array {
buffer.WriteString(`"` + gstr.QuoteMeta(v, `"\`) + `"`)
if k != len(a.array)-1 {
buffer.WriteByte(',')
}
}
buffer.WriteByte(']')
return buffer.String()
2019-04-24 22:23:32 +08:00
}
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
// Note that do not use pointer as its receiver here.
func (a StrArray) MarshalJSON() ([]byte, error) {
a.lazyInit()
return a.TArray.MarshalJSON()
}
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (a *StrArray) UnmarshalJSON(b []byte) error {
a.lazyInit()
return a.TArray.UnmarshalJSON(b)
}
// UnmarshalValue is an interface implement which sets any type of value for array.
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395) This pull request standardizes the use of the Go 1.18+ `any` type alias instead of `interface{}` throughout the codebase. The change improves code readability and aligns with modern Go best practices. The update touches many files, including core data structures, code generation templates, logging utilities, and test data, ensuring consistency across all usages. **Type alias migration to `any`:** * Replaced all instances of `interface{}` with `any` in core data structures such as `garray` and in generated model structs (e.g., `TableUser`, `User1`, `User2`) to modernize type usage. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) * Updated function signatures, method parameters, and return types from `interface{}` to `any` in various parts of the codebase, including code generation, service logic, and logging utilities (e.g., `mlog`). [[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74) [[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) [[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107) [[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121) [[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112) [[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36) [[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74) [[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96) **Generated code and templates:** * Adjusted generated files and code generation templates to output `any` instead of `interface{}` for relevant struct fields and function signatures, ensuring that new code generation aligns with the updated convention. [[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) [[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) **Container and utility updates:** * Refactored the `garray` container implementation and related constructors/methods to use `[]any` instead of `[]interface{}`, along with corresponding function signatures. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52) [[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62) [[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86) [[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97) [[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114) [[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124) [[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143) [[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167) These changes collectively modernize the codebase and prepare it for future Go developments by using the idiomatic `any` type.
2025-08-28 16:53:19 +08:00
func (a *StrArray) UnmarshalValue(value any) error {
a.lazyInit()
return a.TArray.UnmarshalValue(value)
}
2023-04-24 11:44:19 +08:00
// Filter iterates array and filters elements using custom callback function.
// It removes the element from array if callback function `filter` returns true,
// it or else does nothing and continues iterating.
2023-04-12 10:15:11 +08:00
func (a *StrArray) Filter(filter func(index int, value string) bool) *StrArray {
a.lazyInit()
a.TArray.Filter(filter)
2023-04-12 10:15:11 +08:00
return a
}
// FilterEmpty removes all empty string value of the array.
func (a *StrArray) FilterEmpty() *StrArray {
a.lazyInit()
a.TArray.FilterEmpty()
return a
}
2021-06-02 09:42:27 +08:00
// Walk applies a user supplied function `f` to every item of array.
func (a *StrArray) Walk(f func(value string) string) *StrArray {
a.lazyInit()
a.TArray.Walk(f)
return a
}
// IsEmpty checks whether the array is empty.
func (a *StrArray) IsEmpty() bool {
a.lazyInit()
return a.TArray.IsEmpty()
}
2022-05-23 16:51:10 +08:00
// DeepCopy implements interface for deep copy of current type.
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395) This pull request standardizes the use of the Go 1.18+ `any` type alias instead of `interface{}` throughout the codebase. The change improves code readability and aligns with modern Go best practices. The update touches many files, including core data structures, code generation templates, logging utilities, and test data, ensuring consistency across all usages. **Type alias migration to `any`:** * Replaced all instances of `interface{}` with `any` in core data structures such as `garray` and in generated model structs (e.g., `TableUser`, `User1`, `User2`) to modernize type usage. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) * Updated function signatures, method parameters, and return types from `interface{}` to `any` in various parts of the codebase, including code generation, service logic, and logging utilities (e.g., `mlog`). [[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74) [[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) [[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107) [[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121) [[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112) [[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36) [[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74) [[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96) **Generated code and templates:** * Adjusted generated files and code generation templates to output `any` instead of `interface{}` for relevant struct fields and function signatures, ensuring that new code generation aligns with the updated convention. [[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) [[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) **Container and utility updates:** * Refactored the `garray` container implementation and related constructors/methods to use `[]any` instead of `[]interface{}`, along with corresponding function signatures. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52) [[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62) [[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86) [[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97) [[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114) [[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124) [[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143) [[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167) These changes collectively modernize the codebase and prepare it for future Go developments by using the idiomatic `any` type.
2025-08-28 16:53:19 +08:00
func (a *StrArray) DeepCopy() any {
if a == nil {
return nil
}
a.lazyInit()
return &StrArray{
TArray: a.TArray.DeepCopy().(*TArray[string]),
}
2022-05-23 16:51:10 +08:00
}