本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.4KB

  1. // Copyright (C) MongoDB, Inc. 2017-present.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. package bsoncodec
  7. import "fmt"
  8. type mode int
  9. const (
  10. _ mode = iota
  11. mTopLevel
  12. mDocument
  13. mArray
  14. mValue
  15. mElement
  16. mCodeWithScope
  17. mSpacer
  18. )
  19. func (m mode) String() string {
  20. var str string
  21. switch m {
  22. case mTopLevel:
  23. str = "TopLevel"
  24. case mDocument:
  25. str = "DocumentMode"
  26. case mArray:
  27. str = "ArrayMode"
  28. case mValue:
  29. str = "ValueMode"
  30. case mElement:
  31. str = "ElementMode"
  32. case mCodeWithScope:
  33. str = "CodeWithScopeMode"
  34. case mSpacer:
  35. str = "CodeWithScopeSpacerFrame"
  36. default:
  37. str = "UnknownMode"
  38. }
  39. return str
  40. }
  41. // TransitionError is an error returned when an invalid progressing a
  42. // ValueReader or ValueWriter state machine occurs.
  43. type TransitionError struct {
  44. parent mode
  45. current mode
  46. destination mode
  47. }
  48. func (te TransitionError) Error() string {
  49. if te.destination == mode(0) {
  50. return fmt.Sprintf("invalid state transition: cannot read/write value while in %s", te.current)
  51. }
  52. if te.parent == mode(0) {
  53. return fmt.Sprintf("invalid state transition: %s -> %s", te.current, te.destination)
  54. }
  55. return fmt.Sprintf("invalid state transition: %s -> %s; parent %s", te.current, te.destination, te.parent)
  56. }
上海开阖软件有限公司 沪ICP备12045867号-1