Implement route rules

This commit is contained in:
世界
2022-07-02 22:55:10 +08:00
parent 7c57eb70e8
commit 6eae8e361f
40 changed files with 1220 additions and 145 deletions

27
option/listable.go Normal file
View File

@@ -0,0 +1,27 @@
package option
import "encoding/json"
type Listable[T any] []T
func (l *Listable[T]) MarshalJSON() ([]byte, error) {
arrayList := []T(*l)
if len(arrayList) == 1 {
return json.Marshal(arrayList[0])
}
return json.Marshal(arrayList)
}
func (l *Listable[T]) UnmarshalJSON(bytes []byte) error {
err := json.Unmarshal(bytes, (*[]T)(l))
if err == nil {
return nil
}
var singleItem T
err = json.Unmarshal(bytes, &singleItem)
if err != nil {
return err
}
*l = []T{singleItem}
return nil
}