EasyOpHdlResponse.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package easyophdl
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "git.swzry.com/zry/go-zSshRpcServer/server"
  6. "github.com/bitly/go-simplejson"
  7. )
  8. type EasyOpHdlResponse struct {
  9. statusCode int
  10. responseJson string
  11. isErrHappend bool
  12. errMsg string
  13. isUseSimpleJson bool
  14. isUseRegularJson bool
  15. logger zsshrpc_server.SvrLogFunc
  16. simpleJson *simplejson.Json
  17. }
  18. func (this *EasyOpHdlResponse) UseSimpleJson() {
  19. if this.isUseRegularJson {
  20. this.isErrHappend = true
  21. this.errMsg = "Only Can Use One of SimpleJson or RegularJson."
  22. this.doErrLog()
  23. } else {
  24. this.isUseSimpleJson = true
  25. this.simpleJson = simplejson.New()
  26. }
  27. }
  28. func (this *EasyOpHdlResponse) SimplePut(key string, content interface{}) {
  29. if this.isUseSimpleJson {
  30. this.simpleJson.Set(key, content)
  31. } else {
  32. this.isErrHappend = true
  33. this.errMsg = "Call SimplePut Before Declared UseSimpleJson."
  34. this.doErrLog()
  35. }
  36. }
  37. func (this *EasyOpHdlResponse) UseRegularJson(v interface{}) {
  38. if this.isUseSimpleJson {
  39. this.isErrHappend = true
  40. this.errMsg = "Only Can Use One of SimpleJson or RegularJson."
  41. this.doErrLog()
  42. } else {
  43. this.isUseRegularJson = true
  44. jdata, err := json.Marshal(v)
  45. if err != nil {
  46. this.isErrHappend = true
  47. this.errMsg = fmt.Sprint("Failed Encoding Json: ", err)
  48. this.doErrLog()
  49. }
  50. this.responseJson = string(jdata)
  51. }
  52. }
  53. func (this *EasyOpHdlResponse) SetStatusCode(code int) {
  54. this.statusCode = code
  55. }
  56. func (this *EasyOpHdlResponse) doErrLog() {
  57. if this.isErrHappend {
  58. this.logger(zsshrpc_server.SvrLogLevel_WARNING,
  59. fmt.Sprint("Internal Error: ", this.errMsg),
  60. nil,
  61. )
  62. }
  63. }