12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package easyophdl
- import (
- "encoding/json"
- "fmt"
- "git.swzry.com/zry/go-zSshRpcServer/server"
- "github.com/bitly/go-simplejson"
- )
- type EasyOpHdlResponse struct {
- statusCode int
- responseJson string
- isErrHappend bool
- errMsg string
- isUseSimpleJson bool
- isUseRegularJson bool
- logger zsshrpc_server.SvrLogFunc
- simpleJson *simplejson.Json
- }
- func (this *EasyOpHdlResponse) UseSimpleJson() {
- if this.isUseRegularJson {
- this.isErrHappend = true
- this.errMsg = "Only Can Use One of SimpleJson or RegularJson."
- this.doErrLog()
- } else {
- this.isUseSimpleJson = true
- this.simpleJson = simplejson.New()
- }
- }
- func (this *EasyOpHdlResponse) SimplePut(key string, content interface{}) {
- if this.isUseSimpleJson {
- this.simpleJson.Set(key, content)
- } else {
- this.isErrHappend = true
- this.errMsg = "Call SimplePut Before Declared UseSimpleJson."
- this.doErrLog()
- }
- }
- func (this *EasyOpHdlResponse) UseRegularJson(v interface{}) {
- if this.isUseSimpleJson {
- this.isErrHappend = true
- this.errMsg = "Only Can Use One of SimpleJson or RegularJson."
- this.doErrLog()
- } else {
- this.isUseRegularJson = true
- jdata, err := json.Marshal(v)
- if err != nil {
- this.isErrHappend = true
- this.errMsg = fmt.Sprint("Failed Encoding Json: ", err)
- this.doErrLog()
- }
- this.responseJson = string(jdata)
- }
- }
- func (this *EasyOpHdlResponse) SetStatusCode(code int) {
- this.statusCode = code
- }
- func (this *EasyOpHdlResponse) doErrLog() {
- if this.isErrHappend {
- this.logger(zsshrpc_server.SvrLogLevel_WARNING,
- fmt.Sprint("Internal Error: ", this.errMsg),
- nil,
- )
- }
- }
|