feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package conv
import (
"encoding/json"
"strconv"
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
)
// StrToInt64E returns strconv.ParseInt(v, 10, 64)
func StrToInt64(v string) (int64, error) {
return strconv.ParseInt(v, 10, 64)
}
// Int64ToStr returns strconv.FormatInt(v, 10) result
func Int64ToStr(v int64) string {
return strconv.FormatInt(v, 10)
}
// StrToInt64 returns strconv.ParseInt(v, 10, 64)'s value.
// if error occurs, returns defaultValue as result.
func StrToInt64D(v string, defaultValue int64) int64 {
toV, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return defaultValue
}
return toV
}
// DebugJsonToStr
func DebugJsonToStr(v interface{}) string {
b, err := json.Marshal(v)
if err != nil {
return ""
}
return string(b)
}
func BoolToInt(p bool) int {
if p == true {
return 1
}
return 0
}
// BoolToIntPointer returns 1 or 0 as pointer
func BoolToIntPointer(p *bool) *int {
if p == nil {
return nil
}
if *p == true {
return ptr.Of(int(1))
}
return ptr.Of(int(0))
}

View File

@@ -0,0 +1,11 @@
package crypto
import (
"crypto/md5"
"encoding/hex"
)
func MD5HexValue(input string) string {
md5Hash := md5.Sum([]byte(input))
return hex.EncodeToString(md5Hash[:])
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package maps
func ToAnyValue[K comparable, V any](m map[K]V) map[K]any {
n := make(map[K]any, len(m))
for k, v := range m {
n[k] = v
}
return n
}
func TransformKey[K1, K2 comparable, V any](m map[K1]V, f func(K1) K2) map[K2]V {
n := make(map[K2]V, len(m))
for k1, v := range m {
n[f(k1)] = v
}
return n
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ptr
func Of[T any](t T) *T {
return &t
}
func From[T any](p *T) T {
if p != nil {
return *p
}
var t T
return t
}
func FromOrDefault[T any](p *T, def T) T {
if p != nil {
return *p
}
return def
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sets
type Set[T comparable] map[T]struct{}
func FromSlice[T comparable](s []T) Set[T] {
set := make(Set[T], len(s))
for _, elem := range s {
cpElem := elem
set[cpElem] = struct{}{}
}
return set
}
func (s Set[T]) ToSlice() []T {
sl := make([]T, 0, len(s))
for elem := range s {
cpElem := elem
sl = append(sl, cpElem)
}
return sl
}
func (s Set[T]) Contains(elem T) bool {
_, ok := s[elem]
return ok
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package signal
import (
"os"
"os/signal"
"syscall"
)
func WaitExit() {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM)
<-signals
}

View File

@@ -0,0 +1,125 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package slices
func Transform[A, B any](src []A, fn func(A) B) []B {
if src == nil {
return nil
}
dst := make([]B, 0, len(src))
for _, a := range src {
dst = append(dst, fn(a))
}
return dst
}
func TransformWithErrorCheck[A, B any](src []A, fn func(A) (B, error)) ([]B, error) {
if src == nil {
return nil, nil
}
dst := make([]B, 0, len(src))
for _, a := range src {
item, err := fn(a)
if err != nil {
return nil, err
}
dst = append(dst, item)
}
return dst, nil
}
func GroupBy[A, K comparable, V any](src []A, fn func(A) (K, V)) map[K][]V {
if src == nil {
return nil
}
dst := make(map[K][]V, len(src))
for _, a := range src {
k, v := fn(a)
dst[k] = append(dst[k], v)
}
return dst
}
func Unique[T comparable](src []T) []T {
if src == nil {
return nil
}
dst := make([]T, 0, len(src))
m := make(map[T]struct{}, len(src))
for _, s := range src {
if _, ok := m[s]; ok {
continue
}
dst = append(dst, s)
m[s] = struct{}{}
}
return dst
}
func Fill[T any](val T, size int) []T {
slice := make([]T, size)
for i := 0; i < size; i++ {
slice[i] = val
}
return slice
}
func Chunks[T any](s []T, chunkSize int) [][]T {
sliceLen := len(s)
chunks := make([][]T, 0, sliceLen/chunkSize)
for start := 0; start < sliceLen; start += chunkSize {
end := start + chunkSize
if end > sliceLen {
end = sliceLen
}
chunks = append(chunks, s[start:end])
}
return chunks
}
func ToMap[E any, K comparable, V any](src []E, fn func(e E) (K, V)) map[K]V {
if src == nil {
return nil
}
dst := make(map[K]V, len(src))
for _, e := range src {
k, v := fn(e)
dst[k] = v
}
return dst
}
func Reverse[T any](slice []T) []T {
left := 0
right := len(slice) - 1
for left < right {
slice[left], slice[right] = slice[right], slice[left]
left++
right--
}
return slice
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sqlutil
import (
"database/sql/driver"
)
func DriverValue[T any](v T) driver.Valuer {
return value[T]{val: v}
}
type value[T any] struct {
val T
}
func (i value[T]) Value() (driver.Value, error) {
return i.val, nil
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ternary
func IFElse[T any](ok bool, trueValue, falseValue T) T {
if ok {
return trueValue
}
return falseValue
}