mirror of
https://github.com/samuelncui/acp.git
synced 2025-12-23 05:05:15 +00:00
fix: report json marshal error
This commit is contained in:
2
error.go
2
error.go
@@ -37,6 +37,6 @@ func (e *Error) UnmarshalJSON(buf []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
e.Src, e.Dst, e.Err = m.Src, e.Dst, fmt.Errorf(m.Err)
|
||||
e.Src, e.Dst, e.Err = m.Src, m.Dst, fmt.Errorf(m.Err)
|
||||
return nil
|
||||
}
|
||||
|
||||
11
go.mod
11
go.mod
@@ -3,22 +3,23 @@ module github.com/abc950309/acp
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1
|
||||
github.com/deckarep/golang-set/v2 v2.1.0
|
||||
github.com/hashicorp/go-multierror v1.1.1
|
||||
github.com/json-iterator/go v1.1.12
|
||||
github.com/klauspost/cpuid/v2 v2.0.4
|
||||
github.com/minio/sha256-simd v1.0.0
|
||||
github.com/modern-go/reflect2 v1.0.2
|
||||
github.com/schollz/progressbar/v3 v3.10.1
|
||||
github.com/sirupsen/logrus v1.9.0
|
||||
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
|
||||
github.com/hashicorp/errwrap v1.0.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.0.4 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.13 // indirect
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/rivo/uniseg v0.3.4 // indirect
|
||||
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
|
||||
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
|
||||
)
|
||||
|
||||
35
report.go
35
report.go
@@ -6,8 +6,10 @@ import (
|
||||
"sync"
|
||||
"unsafe"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/modern-go/reflect2"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type ReportGetter func() *Report
|
||||
@@ -74,20 +76,45 @@ var (
|
||||
type errValCoder struct{}
|
||||
|
||||
func (*errValCoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
logrus.Infof("IsEmpty %s", spew.Sdump(ptr))
|
||||
val := (*error)(ptr)
|
||||
return *val == nil
|
||||
}
|
||||
|
||||
func (*errValCoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
||||
logrus.Infof("Encode %s", spew.Sdump(ptr))
|
||||
val := (*error)(ptr)
|
||||
stream.WriteString((*val).Error())
|
||||
}
|
||||
|
||||
func (*errValCoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
logrus.Infof("Decode %s", spew.Sdump(ptr))
|
||||
val := (*error)(ptr)
|
||||
*val = fmt.Errorf(iter.ReadString())
|
||||
}
|
||||
|
||||
var (
|
||||
errorType2 reflect2.Type
|
||||
)
|
||||
|
||||
type reportJSONExtension struct {
|
||||
jsoniter.DummyExtension
|
||||
}
|
||||
|
||||
func (*reportJSONExtension) CreateDecoder(typ reflect2.Type) jsoniter.ValDecoder {
|
||||
if typ.Implements(errorType2) {
|
||||
return &errValCoder{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*reportJSONExtension) CreateEncoder(typ reflect2.Type) jsoniter.ValEncoder {
|
||||
if typ.Implements(errorType2) {
|
||||
return &errValCoder{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
reportJSON = jsoniter.Config{
|
||||
EscapeHTML: true,
|
||||
@@ -96,10 +123,6 @@ func init() {
|
||||
}.Froze()
|
||||
|
||||
var emptyErr error
|
||||
reportJSON.RegisterExtension(jsoniter.EncoderExtension{
|
||||
reflect2.TypeOf(emptyErr): &errValCoder{},
|
||||
})
|
||||
reportJSON.RegisterExtension(jsoniter.DecoderExtension{
|
||||
reflect2.TypeOf(emptyErr): &errValCoder{},
|
||||
})
|
||||
errorType2 = reflect2.TypeOfPtr(&emptyErr).Elem()
|
||||
reportJSON.RegisterExtension(&reportJSONExtension{})
|
||||
}
|
||||
|
||||
21
report_test.go
Normal file
21
report_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package acp
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/modern-go/reflect2"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func TestErrorJSONMarshal(t *testing.T) {
|
||||
m := map[string]error{}
|
||||
m["test"] = syscall.EROFS
|
||||
|
||||
var err error
|
||||
logrus.Infof("get error type %s", spew.Sdump(reflect2.TypeOfPtr(&err).Elem()))
|
||||
|
||||
buf, _ := reportJSON.Marshal(m)
|
||||
logrus.Infof("get json %s", buf)
|
||||
}
|
||||
Reference in New Issue
Block a user