github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/util/json/iterator.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package json
    12  
    13  // ObjectIterator is an iterator to access the key value pair of an object in
    14  // sorted order based on key.
    15  type ObjectIterator struct {
    16  	src jsonObject
    17  	idx int
    18  }
    19  
    20  func newObjectIterator(src jsonObject) *ObjectIterator {
    21  	return &ObjectIterator{
    22  		src: src,
    23  		idx: -1,
    24  	}
    25  }
    26  
    27  // Next updates the cursor and returns whether the next pair exists.
    28  func (it *ObjectIterator) Next() bool {
    29  	if it.idx >= len(it.src)-1 {
    30  		return false
    31  	}
    32  	it.idx++
    33  	return true
    34  }
    35  
    36  // Key returns key of the current pair.
    37  func (it *ObjectIterator) Key() string {
    38  	return string(it.src[it.idx].k)
    39  }
    40  
    41  // Value returns value of the current pair
    42  func (it *ObjectIterator) Value() JSON {
    43  	return it.src[it.idx].v
    44  }