This commit is contained in:
suyl
2021-07-13 17:35:14 +08:00
parent 22100c561d
commit faaaa9566b
2 changed files with 26 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/jx-print/globals"
"git.rosy.net.cn/jx-print/model"
"github.com/dchest/captcha"
@@ -166,3 +167,27 @@ func BuildErr(errs []error) (err error) {
}
return fmt.Errorf(errStr.String())
}
func Format4Output(obj interface{}, isSingleLine bool) (retVal string) {
retVal, ok := obj.(string)
if !ok {
var (
result []byte
err error
)
if isSingleLine {
if result, err = json.Marshal(obj); err == nil {
retVal = string(result)
}
} else {
if result, err = json.MarshalIndent(obj, "", "\t"); err == nil {
retVal = string(result)
}
}
if err != nil {
baseapi.SugarLogger.Infof("Format4Output Marshal:%v failed with error:%v", obj, err)
retVal = fmt.Sprintf("%v", obj)
}
}
return retVal
}