Compare commits
4 Commits
entgo/v1.1
...
entgo/v1.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b27c96f932 | ||
|
|
836f4e2461 | ||
|
|
5717d4aefe | ||
|
|
62142a6836 |
21
entgo/mixin/creator_id.go
Normal file
21
entgo/mixin/creator_id.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package mixin
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
_mixin "entgo.io/ent/schema/mixin"
|
||||
)
|
||||
|
||||
type CreatorId struct {
|
||||
_mixin.Schema
|
||||
}
|
||||
|
||||
func (CreatorId) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Uint64("creator_id").
|
||||
Comment("创建者用户ID").
|
||||
Immutable().
|
||||
Optional().
|
||||
Nillable(),
|
||||
}
|
||||
}
|
||||
37
entgo/mixin/snowflake_id.go
Normal file
37
entgo/mixin/snowflake_id.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package mixin
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
|
||||
"github.com/tx7do/go-utils/sonyflake"
|
||||
)
|
||||
|
||||
type SnowflackId struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
func (SnowflackId) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Uint64("id").
|
||||
Comment("id").
|
||||
DefaultFunc(sonyflake.GenerateSonyflake).
|
||||
Positive().
|
||||
Immutable().
|
||||
StructTag(`json:"id,omitempty"`).
|
||||
SchemaType(map[string]string{
|
||||
dialect.MySQL: "bigint",
|
||||
dialect.Postgres: "bigint",
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the SnowflackId.
|
||||
func (SnowflackId) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("id"),
|
||||
}
|
||||
}
|
||||
33
entgo/mixin/string_id.go
Normal file
33
entgo/mixin/string_id.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package mixin
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type StringId struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
func (StringId) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("id").
|
||||
Comment("id").
|
||||
MaxLen(25).
|
||||
NotEmpty().
|
||||
Unique().
|
||||
Immutable().
|
||||
Match(regexp.MustCompile("^[0-9a-zA-Z_\\-]+$")).
|
||||
StructTag(`json:"id,omitempty"`),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the StringId.
|
||||
func (StringId) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("id"),
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ func (SwitchStatus) Fields() []ent.Field {
|
||||
// dialect.MySQL: "switch_status",
|
||||
// dialect.Postgres: "switch_status",
|
||||
//}).
|
||||
Default("ON").
|
||||
Values(
|
||||
"OFF",
|
||||
"ON",
|
||||
|
||||
2
go.mod
2
go.mod
@@ -4,6 +4,7 @@ go 1.20
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.4.0
|
||||
github.com/gosimple/slug v1.13.1
|
||||
github.com/sony/sonyflake v1.2.0
|
||||
github.com/stretchr/testify v1.8.4
|
||||
golang.org/x/crypto v0.14.0
|
||||
@@ -11,6 +12,7 @@ require (
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/gosimple/unidecode v1.0.1 // indirect
|
||||
github.com/kr/pretty v0.3.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/rogpeppe/go-internal v1.10.0 // indirect
|
||||
|
||||
4
go.sum
4
go.sum
@@ -3,6 +3,10 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
|
||||
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gosimple/slug v1.13.1 h1:bQ+kpX9Qa6tHRaK+fZR0A0M2Kd7Pa5eHPPsb1JpHD+Q=
|
||||
github.com/gosimple/slug v1.13.1/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ=
|
||||
github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
|
||||
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
|
||||
29
slug/slug.go
Normal file
29
slug/slug.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package slug
|
||||
|
||||
import (
|
||||
"github.com/gosimple/slug"
|
||||
)
|
||||
|
||||
// Generate 生成短链接
|
||||
func Generate(input string) string {
|
||||
slug.Lowercase = true
|
||||
return slug.MakeLang(input, "en")
|
||||
}
|
||||
|
||||
// GenerateCaseSensitive 生成大小写敏感的短链接
|
||||
func GenerateCaseSensitive(input string) string {
|
||||
slug.Lowercase = false
|
||||
return slug.MakeLang(input, "en")
|
||||
}
|
||||
|
||||
// GenerateEnglish 生成英文短链接
|
||||
func GenerateEnglish(input string) string {
|
||||
slug.Lowercase = true
|
||||
return slug.MakeLang(input, "en")
|
||||
}
|
||||
|
||||
// GenerateGerman 生成德文短链接
|
||||
func GenerateGerman(input string) string {
|
||||
slug.Lowercase = true
|
||||
return slug.MakeLang(input, "de")
|
||||
}
|
||||
88
slug/slug_test.go
Normal file
88
slug/slug_test.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package slug
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/gosimple/slug"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGoSimple(t *testing.T) {
|
||||
// 俄文
|
||||
text := slug.Make("Hellö Wörld хелло ворлд")
|
||||
assert.Equal(t, text, "hello-world-khello-vorld")
|
||||
fmt.Println(text)
|
||||
|
||||
// 繁体中文
|
||||
someText := slug.Make("影師")
|
||||
assert.Equal(t, someText, "ying-shi")
|
||||
fmt.Println(someText)
|
||||
|
||||
// 简体中文
|
||||
cnText := slug.Make("天下太平")
|
||||
assert.Equal(t, cnText, "tian-xia-tai-ping")
|
||||
fmt.Println(cnText)
|
||||
|
||||
// 英文
|
||||
enText := slug.MakeLang("This & that", "en")
|
||||
assert.Equal(t, enText, "this-and-that")
|
||||
fmt.Println(enText)
|
||||
|
||||
// 德文
|
||||
deText := slug.MakeLang("Diese & Dass", "de")
|
||||
assert.Equal(t, deText, "diese-und-dass")
|
||||
fmt.Println(deText)
|
||||
|
||||
// 保持大小写
|
||||
slug.Lowercase = false
|
||||
deUppercaseText := slug.MakeLang("Diese & Dass", "de")
|
||||
assert.Equal(t, deUppercaseText, "Diese-und-Dass")
|
||||
fmt.Println(deUppercaseText)
|
||||
|
||||
// 字符替换
|
||||
slug.CustomSub = map[string]string{
|
||||
"water": "sand",
|
||||
}
|
||||
textSub := slug.Make("water is hot")
|
||||
assert.Equal(t, textSub, "sand-is-hot")
|
||||
fmt.Println(textSub)
|
||||
}
|
||||
|
||||
func TestGenerate(t *testing.T) {
|
||||
// 俄文
|
||||
text := Generate("Hellö Wörld хелло ворлд")
|
||||
assert.Equal(t, text, "hello-world-khello-vorld")
|
||||
fmt.Println(text)
|
||||
|
||||
// 繁体中文
|
||||
someText := Generate("影師")
|
||||
assert.Equal(t, someText, "ying-shi")
|
||||
fmt.Println(someText)
|
||||
|
||||
// 简体中文
|
||||
cnText := Generate("天下太平")
|
||||
assert.Equal(t, cnText, "tian-xia-tai-ping")
|
||||
fmt.Println(cnText)
|
||||
|
||||
// 英文
|
||||
enText := GenerateEnglish("This & that")
|
||||
assert.Equal(t, enText, "this-and-that")
|
||||
fmt.Println(enText)
|
||||
|
||||
enText = GenerateCaseSensitive("This & That")
|
||||
assert.Equal(t, enText, "This-and-That")
|
||||
fmt.Println(enText)
|
||||
|
||||
// 德文
|
||||
deText := GenerateGerman("Diese & Dass")
|
||||
assert.Equal(t, deText, "diese-und-dass")
|
||||
fmt.Println(deText)
|
||||
|
||||
slug.CustomSub = map[string]string{
|
||||
"water": "sand",
|
||||
}
|
||||
textSub := Generate("water is hot")
|
||||
assert.Equal(t, textSub, "sand-is-hot")
|
||||
fmt.Println(textSub)
|
||||
}
|
||||
Reference in New Issue
Block a user