From 9e9c3c810faa3a35dec447d08ca22f5e4a230867 Mon Sep 17 00:00:00 2001 From: lelemka0 <20322043+lelemka0@users.noreply.github.com> Date: Fri, 6 May 2022 11:43:53 +0800 Subject: [PATCH] fixed: make log api unblocked --- common/structure/structure.go | 14 ++++++++++++-- log/log.go | 20 +++++++------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/common/structure/structure.go b/common/structure/structure.go index 6b038bd2..e74c349a 100644 --- a/common/structure/structure.go +++ b/common/structure/structure.go @@ -159,9 +159,19 @@ func (d *Decoder) decodeSlice(name string, data any, val reflect.Value) error { for valSlice.Len() <= i { valSlice = reflect.Append(valSlice, reflect.Zero(valElemType)) } - currentField := valSlice.Index(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 { return err } diff --git a/log/log.go b/log/log.go index 58e122ef..f1053f44 100644 --- a/log/log.go +++ b/log/log.go @@ -10,8 +10,8 @@ import ( ) var ( - logCh = make(chan *Event) - source = observable.NewObservable[*Event](logCh) + logCh = make(chan Event) + source = observable.NewObservable[Event](logCh) level = INFO ) @@ -57,12 +57,12 @@ func Fatalln(format string, v ...any) { log.Fatalf(format, v...) } -func Subscribe() observable.Subscription[*Event] { +func Subscribe() observable.Subscription[Event] { sub, _ := source.Subscribe() return sub } -func UnSubscribe(sub observable.Subscription[*Event]) { +func UnSubscribe(sub observable.Subscription[Event]) { source.UnSubscribe(sub) } @@ -74,7 +74,7 @@ func SetLevel(newLevel LogLevel) { level = newLevel } -func print(data *Event) { +func print(data Event) { if data.LogLevel < level { return } @@ -91,15 +91,9 @@ func print(data *Event) { } } -func newLog(logLevel LogLevel, format string, v ...any) *Event { - return &Event{ +func newLog(logLevel LogLevel, format string, v ...any) Event { + return Event{ LogLevel: logLevel, Payload: fmt.Sprintf(format, v...), } } - -func PrintLog(logLevel LogLevel, format string, v ...interface{}) { - event := newLog(logLevel, format, v...) - logCh <- event - print(event) -}