parse_session.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package jsonutils
  15. import (
  16. "fmt"
  17. )
  18. type sNodeReferer struct {
  19. node JSONObject
  20. pointers []*sJSONPointer
  21. }
  22. type sJsonParseSession struct {
  23. objectMap map[int]*sNodeReferer
  24. }
  25. func newJsonParseSession() *sJsonParseSession {
  26. return &sJsonParseSession{
  27. objectMap: make(map[int]*sNodeReferer),
  28. }
  29. }
  30. func (s *sJsonParseSession) saveReferer(nodeId int, ptr *sJSONPointer) {
  31. if nr, ok := s.objectMap[nodeId]; ok {
  32. nr.pointers = append(nr.pointers, ptr)
  33. } else {
  34. s.objectMap[nodeId] = &sNodeReferer{
  35. pointers: []*sJSONPointer{ptr},
  36. }
  37. }
  38. }
  39. func (s *sJsonParseSession) saveNode(nodeId int, node JSONObject) {
  40. if nr, ok := s.objectMap[nodeId]; ok {
  41. if nr.node != nil {
  42. panic(fmt.Sprintf("nodeId %d alreayd exists: %s != %s", nodeId, nr.node, node))
  43. } else {
  44. nr.node = node
  45. }
  46. } else {
  47. s.objectMap[nodeId] = &sNodeReferer{
  48. node: node,
  49. pointers: nil,
  50. }
  51. }
  52. }