fixed: make log api unblocked

This commit is contained in:
lelemka0 2022-05-06 11:43:53 +08:00 committed by Meta
parent 463101aec1
commit 9e9c3c810f
2 changed files with 19 additions and 15 deletions

View file

@ -159,9 +159,19 @@ func (d *Decoder) decodeSlice(name string, data any, val reflect.Value) error {
for valSlice.Len() <= i { for valSlice.Len() <= i {
valSlice = reflect.Append(valSlice, reflect.Zero(valElemType)) valSlice = reflect.Append(valSlice, reflect.Zero(valElemType))
} }
currentField := valSlice.Index(i)
fieldName := fmt.Sprintf("%s[%d]", name, i) fieldName := fmt.Sprintf("%s[%d]", name, i)
if currentData == nil {
// in weakly type mode, null will convert to zero value
if d.option.WeaklyTypedInput {
continue
}
// in non-weakly type mode, null will convert to nil if element's zero value is nil, otherwise return an error
if elemKind := valElemType.Kind(); elemKind == reflect.Map || elemKind == reflect.Slice {
continue
}
return fmt.Errorf("'%s' can not be null", fieldName)
}
currentField := valSlice.Index(i)
if err := d.decode(fieldName, currentData, currentField); err != nil { if err := d.decode(fieldName, currentData, currentField); err != nil {
return err return err
} }

View file

@ -10,8 +10,8 @@ import (
) )
var ( var (
logCh = make(chan *Event) logCh = make(chan Event)
source = observable.NewObservable[*Event](logCh) source = observable.NewObservable[Event](logCh)
level = INFO level = INFO
) )
@ -57,12 +57,12 @@ func Fatalln(format string, v ...any) {
log.Fatalf(format, v...) log.Fatalf(format, v...)
} }
func Subscribe() observable.Subscription[*Event] { func Subscribe() observable.Subscription[Event] {
sub, _ := source.Subscribe() sub, _ := source.Subscribe()
return sub return sub
} }
func UnSubscribe(sub observable.Subscription[*Event]) { func UnSubscribe(sub observable.Subscription[Event]) {
source.UnSubscribe(sub) source.UnSubscribe(sub)
} }
@ -74,7 +74,7 @@ func SetLevel(newLevel LogLevel) {
level = newLevel level = newLevel
} }
func print(data *Event) { func print(data Event) {
if data.LogLevel < level { if data.LogLevel < level {
return return
} }
@ -91,15 +91,9 @@ func print(data *Event) {
} }
} }
func newLog(logLevel LogLevel, format string, v ...any) *Event { func newLog(logLevel LogLevel, format string, v ...any) Event {
return &Event{ return Event{
LogLevel: logLevel, LogLevel: logLevel,
Payload: fmt.Sprintf(format, v...), Payload: fmt.Sprintf(format, v...),
} }
} }
func PrintLog(logLevel LogLevel, format string, v ...interface{}) {
event := newLog(logLevel, format, v...)
logCh <- event
print(event)
}