sync: 对齐小程序 v1.0.2 生产代码并脱敏
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package batchmetadata
|
||||
|
||||
import "github.com/gogf/gf/v2/errors/gerror"
|
||||
|
||||
const (
|
||||
Console = "console"
|
||||
MiniProgram = "mini_program"
|
||||
API = "api"
|
||||
System = "system"
|
||||
Legacy = "legacy"
|
||||
Unknown = 0
|
||||
Single = 1
|
||||
Bulk = 2
|
||||
)
|
||||
|
||||
// NormalizeMode validates the explicit execution size supplied by the business entry point.
|
||||
// Execution size is request metadata and must not be inferred from context or result counts.
|
||||
func NormalizeMode(requested int) int {
|
||||
if requested == Single || requested == Bulk {
|
||||
return requested
|
||||
}
|
||||
return Unknown
|
||||
}
|
||||
|
||||
func ValidChannel(value string) bool {
|
||||
switch value {
|
||||
case Console, MiniProgram, API, System, Legacy:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func Channel(value string) string {
|
||||
if ValidChannel(value) {
|
||||
return value
|
||||
}
|
||||
return Legacy
|
||||
}
|
||||
|
||||
func ValidMode(value int) bool { return value >= Unknown && value <= Bulk }
|
||||
|
||||
// ValidateCreation rejects missing attribution for new work; historical reads
|
||||
// and resumed work continue to use Channel/NormalizeMode for compatibility.
|
||||
func ValidateCreation(channel string, mode int) error {
|
||||
if !ValidChannel(channel) || channel == Legacy {
|
||||
return gerror.New("新建批次发起渠道无效")
|
||||
}
|
||||
if mode != Single && mode != Bulk {
|
||||
return gerror.New("新建批次必须指定单卡或批量操作规模")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package batchmetadata
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestValidateCreation(t *testing.T) {
|
||||
for _, channel := range []string{Console, MiniProgram, API, System} {
|
||||
for _, mode := range []int{Single, Bulk} {
|
||||
if err := ValidateCreation(channel, mode); err != nil {
|
||||
t.Fatal(channel, mode, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, channel := range []string{"", Legacy} {
|
||||
if err := ValidateCreation(channel, Single); err == nil {
|
||||
t.Fatal("missing new batch attribution accepted", channel)
|
||||
}
|
||||
}
|
||||
if ValidateCreation("worker", Single) == nil {
|
||||
t.Fatal("invalid source accepted")
|
||||
}
|
||||
for _, mode := range []int{-1, Unknown, 3} {
|
||||
if ValidateCreation(Console, mode) == nil {
|
||||
t.Fatal("invalid new execution size accepted", mode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetadata(t *testing.T) {
|
||||
if NormalizeMode(0) != Unknown {
|
||||
t.Fatal("unspecified entry mode must remain unknown")
|
||||
}
|
||||
if NormalizeMode(Single) != Single {
|
||||
t.Fatal("single entry mode lost")
|
||||
}
|
||||
if NormalizeMode(Bulk) != Bulk {
|
||||
t.Fatal("bulk entry mode lost")
|
||||
}
|
||||
if NormalizeMode(99) != Unknown {
|
||||
t.Fatal("invalid entry mode accepted")
|
||||
}
|
||||
for _, c := range []string{Console, MiniProgram, API, System, Legacy} {
|
||||
if Channel(c) != c {
|
||||
t.Fatal(c)
|
||||
}
|
||||
}
|
||||
if Channel("worker") != Legacy || Channel("") != Legacy {
|
||||
t.Fatal("unknown source was attributed")
|
||||
}
|
||||
if ValidMode(-1) || ValidMode(3) {
|
||||
t.Fatal("invalid mode accepted")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user