Skip to content

Commit cad5a1c

Browse files
committed
dto: 基本目录结构&对象定义
--issue=#1
1 parent 421f659 commit cad5a1c

21 files changed

+353
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
.idea

.golangci.yml

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# details in https://golangci-lint.run/usage/configuration/
2+
run:
3+
timeout: 3m
4+
5+
output:
6+
sort-results: true
7+
8+
linters-settings:
9+
funlen:
10+
lines: 80
11+
statements: 80
12+
goconst:
13+
min-len: 2
14+
min-occurrences: 2
15+
gocyclo:
16+
min-complexity: 10
17+
goimports:
18+
local-prefixes: tencent-connect/
19+
govet:
20+
check-shadowing: true
21+
lll:
22+
line-length: 120
23+
tab-width: 4
24+
gocritic:
25+
enabled-checks:
26+
- nestingReduce
27+
settings:
28+
nestingReduce:
29+
bodyWidth: 5
30+
31+
linters:
32+
disable-all: true
33+
enable:
34+
- revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint.
35+
- deadcode
36+
- funlen
37+
- goconst
38+
- gocyclo
39+
- gofmt
40+
- ineffassign
41+
- staticcheck
42+
- structcheck # 当非导出结构嵌入另一个结构, 前一个结构被使用就不会监测到, 这个需要每个业务自己屏蔽
43+
- typecheck
44+
- goimports
45+
- gosimple
46+
- govet
47+
- lll
48+
- rowserrcheck
49+
- errcheck
50+
- unused
51+
- varcheck
52+
- gocritic
53+
- dupl
54+
- ifshort
55+
- whitespace
56+
- bodyclose
57+
- sqlclosecheck
58+
59+
issues:
60+
exclude-use-default: true
61+
62+
# The list of ids of default excludes to include or disable. By default it's empty.
63+
# 为了避免 exclude default 导致一些问题被隐藏,所以需要自己声明不想要排除的问题,在 https://golangci-lint.run/usage/configuration/ 搜索 --exclude-use-default
64+
include:
65+
- EXC0004 # govet (possible misuse of unsafe.Pointer|should have signature)
66+
- EXC0005 # staticcheck ineffective break statement. Did you mean to break out of the outer loop
67+
- EXC0011 # stylecheck (comment on exported (method|function|type|const)|should have( a package)? comment|comment should be of the form)
68+
- EXC0012 # revive
69+
- EXC0013 # revive
70+
- EXC0014 # revive
71+
- EXC0015 # revive
72+
73+
exclude-rules:
74+
- path: _test\.go
75+
linters:
76+
- funlen # 规范说单测函数,单个函数可以到160行,但是工具不好做区分处理,这里就直接不检查单测的函数长度
77+
- lll
78+
- goconst
79+
- gocyclo
80+
- golint
81+
- staticcheck
82+
- revive
83+
- dupl
84+
- ifshort
85+
- linters:
86+
- staticcheck
87+
text: "SA1019: package github.com/golang/protobuf" # 它会说 'SA1019: package github.com/golang/protobuf/proto is deprecated: Use the "google.golang.org/protobuf/proto" package instead.', 但是这个库更更新很稳定
88+
- linters:
89+
- staticcheck
90+
text: "SA6002: argument should be pointer-like to avoid allocations" # sync.pool.Put(buf), slice `var buf []byte` will tiger this
91+
- linters:
92+
- staticcheck
93+
text: "SA3000: TestMain should call os.Exit to set exit code"
94+
- linters:
95+
- lll
96+
source: "^//go:generate " # Exclude lll issues for long lines with go:generate
97+
98+
max-same-issues: 0
99+
new: false
100+
max-issues-per-linter: 0
101+
fix: false # 如果设置为 true,会导致一些问题不被扫描出来,而且也不会自动 fix,而且在ci上跑,会莫名的报错
102+
103+
service:
104+
golangci-lint-version: 1.23.x

botgo.go

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package botgo 是一个QQ频道机器人 sdk 的 golang 实现
2+
package botgo

demo/.keep

Whitespace-only changes.

dto/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## dto
2+
3+
与 openapi/websocket 通信时所使用的对象。

dto/channel.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package dto
2+
3+
// ChannelType 频道类型定义
4+
type ChannelType int
5+
6+
// 子频道类型定义
7+
const (
8+
ChannelTypeText ChannelType = iota
9+
_
10+
ChannelTypeVoice
11+
_
12+
ChannelTypeCategory
13+
ChannelTypeLive // 直播子频道
14+
ChannelTypeApplication // 应用子频道
15+
)
16+
17+
// Channel 频道结构定义
18+
type Channel struct {
19+
// 频道ID
20+
ID string `json:"id"`
21+
// 群ID
22+
GuildID string `json:"guild_id"`
23+
// 频道名称
24+
Name string `json:"name"`
25+
// 频道类型
26+
Type ChannelType `json:"type"`
27+
// 排序位置
28+
Position int `json:"position"`
29+
// 父频道的ID
30+
ParentID string `json:"parent_id"`
31+
// 拥有者ID
32+
OwnerID string `json:"owner_id"`
33+
}

dto/duration.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package dto
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"time"
7+
)
8+
9+
// Duration 支持能够直接配置中解析出来 time.Duration 类型的数据
10+
// 需要实现对应类型的 Unmarshaler 接口
11+
type Duration time.Duration
12+
13+
// UnmarshalJSON 实现json的解析接口
14+
func (d *Duration) UnmarshalJSON(bytes []byte) error {
15+
var s = strings.Trim(string(bytes), "\"'")
16+
t, err := time.ParseDuration(s)
17+
if err != nil {
18+
return fmt.Errorf("failed to parse '%s' to time.Duration: %v", s, err)
19+
}
20+
21+
*d = Duration(t)
22+
return nil
23+
}

dto/guild.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package dto
2+
3+
// Guild 频道结构定义
4+
type Guild struct {
5+
// 频道ID(与客户端上看到的频道ID不同)
6+
ID string `json:"id"`
7+
// 频道名称
8+
Name string `json:"name"`
9+
// 频道头像
10+
Icon string `json:"icon"`
11+
// 拥有者ID
12+
OwnerID string `json:"owner_id"`
13+
// 是否为拥有者
14+
IsOwner bool `json:"owner"`
15+
// 成员数量
16+
MemberCount int `json:"member_count"`
17+
// 最大成员数目
18+
MaxMembers int64 `json:"max_members"`
19+
// 频道描述
20+
Desc string `json:"description"`
21+
// 当前用户加入群的时间
22+
// 此字段只在GUILD_CREATE事件中使用
23+
JoinedAt Timestamp `json:"joined_at"`
24+
// 频道列表
25+
Channels []*Channel `json:"channels"`
26+
// 游戏绑定公会区服ID
27+
UnionWorldID string `json:"union_world_id"`
28+
// 游戏绑定公会/战队ID
29+
UnionOrgID string `json:"union_org_id"`
30+
}

dto/member.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dto
2+
3+
// Member 群成员
4+
type Member struct {
5+
GuildID string `json:"guild_id"`
6+
JoinedAt Timestamp `json:"joined_at"`
7+
Nick string `json:"nick"`
8+
User *User `json:"user"`
9+
Roles []string `json:"roles"`
10+
Deaf bool `json:"deaf"`
11+
Mute bool `json:"mute"`
12+
}

dto/timestamp.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package dto
2+
3+
import "time"
4+
5+
// Timestamp 时间戳
6+
type Timestamp string
7+
8+
// Time 时间字符串格式转换
9+
func (t Timestamp) Time() (time.Time, error) {
10+
return time.Parse(time.RFC3339, string(t))
11+
}

dto/user.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package dto
2+
3+
// User 用户
4+
type User struct {
5+
ID string `json:"id"`
6+
Username string `json:"username"`
7+
Avatar string `json:"avatar"`
8+
Bot bool `json:"bot"`
9+
UnionOpenID string `json:"union_openid"` // 特殊关联应用的 openid
10+
UnionUserAccount string `json:"union_user_account"` // 机器人关联的用户信息,与union_openid关联的应用是同一个
11+
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/tencent-connect/botgo
2+
3+
go 1.16

internal/.keep

Whitespace-only changes.

oauth.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package botgo

openapi.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package botgo

scripts/git_hooks/commit-msg

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
# 检查是否携带了 scope:
4+
COMMIT_MSG=$(cat $1 | grep -E "^.+:\s.+(\s|\S)*")
5+
if [ -z "$COMMIT_MSG" ]; then
6+
echo -e "提交信息不符合标准规范, 请使用规范格式, Example: \n\nserver: 增加某某错误处理处理 \n\n--issue=#123"
7+
exit 1
8+
fi
9+
10+
# 检查是否携带了需求ID
11+
COMMIT_MSG=$(cat $1 | grep -E "\-\-(bug|story|task|issue)=(\#)?([0-9])+")
12+
if [ -z "$COMMIT_MSG" ]; then
13+
echo -e "提交信息不符合标准规范, 请使用规范格式, Example: \n\nserver: 增加某某错误处理处理 \n\n--issue=#123"
14+
exit 1
15+
fi
16+
17+
# 检查第一行是否有需求ID
18+
COMMIT_MSG=$(head -n 1 $1 | grep -E "\-\-(bug|story|task|issue)=(\#)?([0-9])+")
19+
if [ -n "$COMMIT_MSG" ]; then
20+
echo "请不要在标题携带 bug/story/task/issue 等 \n\n"
21+
echo "${COMMIT_MSG}"
22+
exit 1
23+
fi
24+
25+
# 检查第一行是否超长,75 字节
26+
COMMIT_MSG=$(head -n 1 $1)
27+
if [ ${#COMMIT_MSG} -gt 75 ]; then
28+
echo "CommitMsg 第一行过长,请保持在 75 字节以内 \n\n"
29+
echo "${COMMIT_MSG}"
30+
exit 1
31+
fi
32+
33+
exit 0

scripts/git_hooks/install.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
git config core.hooksPath scripts/git_hooks && echo ">> local githook has been set" && git config --get core.hooksPath

scripts/git_hooks/pre-commit

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
4+
# 检查当前目录下是否有 golang 文件,如果有则执行 golangci-lint run
5+
go_lint() {
6+
# 当前目录是否有 go 文件
7+
if [ -z "$(search_go_mod)" ]; then
8+
exit 0
9+
fi
10+
echo "发现 go mod,执行 golint 检查" && golangci-lint version && golangci-lint run
11+
}
12+
13+
# 递归向上寻找 go.mod
14+
search_go_mod() {
15+
pwd=$(pwd)
16+
git_top=$(git rev-parse --show-toplevel)
17+
18+
go_mod=$(find . -maxdepth 1 -name "go.mod")
19+
if [ -n "$go_mod" ];then
20+
echo "$go_mod"
21+
fi
22+
23+
if [ "$pwd" == "$git_top" ]; then
24+
return 0
25+
fi
26+
27+
up=$(git rev-parse --show-cdup)
28+
if [ -n "$up" ]; then
29+
cd $up && search_go_mod
30+
fi
31+
}
32+
33+
go_lint

scripts/git_hooks/prepare-commit-msg

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
3+
# 获取分支名
4+
git_branch() {
5+
git rev-parse --abbrev-ref HEAD
6+
}
7+
8+
# 自动给 commit 追加--story, --bug等
9+
auto_append_id() {
10+
branch=$(git_branch)
11+
idstr=$(echo "$branch" | grep -oE "(issue)_([0-9]*)$")
12+
# 分支名中存在 story 等关键词,自动替换为 --story=xxx
13+
if [ -n "$idstr" ]; then
14+
append_str="--${idstr//_/=#}"
15+
echo -e "\n$append_str" >> "$1"
16+
fi
17+
}
18+
19+
# 如果未补充git type,自动在message前补充 git type
20+
# https://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html
21+
auto_prepend_type() {
22+
checkScope=$(cat $1 | grep -E "^.+:\s.+(\s|\S)*")
23+
if [ -n "$checkScope" ]; then
24+
return
25+
fi
26+
prepend_str="$(gen_scope)"": "
27+
content=$(cat "$1")
28+
echo "$prepend_str""$content" > "$1"
29+
}
30+
31+
# 根据提交的变更,自动生成 scope
32+
gen_scope(){
33+
scope=$(get_change_paths | uniq -c | sort -r | head -1 | awk '{print $2}')
34+
echo "$scope"
35+
}
36+
37+
# 获取修改内容的路径
38+
get_change_paths(){
39+
# 提取已经 git add 的修改内容,并且判断,如果目录层级大于2,则提取二级目录名,否则使用以及目录名或者根目录文件名
40+
git status --porcelain | grep -E "^(M|A|D|R|C|U)" | awk '{print $2}' | awk -F/ '{if (NF>2) print $(NF-2)"/"$(NF-1); else print $1}'
41+
}
42+
43+
auto_prepend_type "$1"
44+
auto_append_id "$1"

version.go

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package botgo
2+
3+
// Version sdk 版本
4+
const Version = "0.0.1"

websocket.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package botgo

0 commit comments

Comments
 (0)