diff --git a/edit.go b/edit.go index e10228d..b08d945 100644 --- a/edit.go +++ b/edit.go @@ -30,7 +30,7 @@ type Edit struct { // insert the value (given the existing properties, in order). // // Source: https://github.com/Microsoft/vscode/blob/c0bc1ace7ca3ce2d6b1aeb2bde9d1bb0f4b4bae6/src/vs/base/common/jsonEdit.ts#L14 -func ComputePropertyEdit(text string, path Path, value interface{}, insertionIndex func(properties []string) int, options FormatOptions) ([]Edit, []ParseErrorCode, error) { +func ComputePropertyEdit(text string, path Path, value any, insertionIndex func(properties []string) int, options FormatOptions) ([]Edit, []ParseErrorCode, error) { if value == nil { value = json.RawMessage("null") // otherwise would remove property } @@ -45,7 +45,7 @@ func ComputePropertyRemoval(text string, path Path, options FormatOptions) ([]Ed return computePropertyEdit(text, path, nil, nil, options) } -func computePropertyEdit(text string, path Path, valueObj interface{}, insertionIndex func(properties []string) int, options FormatOptions) ([]Edit, []ParseErrorCode, error) { +func computePropertyEdit(text string, path Path, valueObj any, insertionIndex func(properties []string) int, options FormatOptions) ([]Edit, []ParseErrorCode, error) { // Tolerate errors in value if it's json.RawMessage. var value string if v, ok := valueObj.(json.RawMessage); ok { diff --git a/edit_test.go b/edit_test.go index 7f0ea72..65382fa 100644 --- a/edit_test.go +++ b/edit_test.go @@ -20,7 +20,7 @@ func TestComputePropertyEdit(t *testing.T) { type testCase struct { input string path Path - value interface{} + value any remove bool insertIndex func(properties []string) int options *FormatOptions diff --git a/format.go b/format.go index c210aab..775d94f 100644 --- a/format.go +++ b/format.go @@ -154,10 +154,7 @@ func (f *formatter) format(rangeStart, rangeEnd int) (editOperations []Edit) { } func (f *formatter) newLineAndIndent() string { - n := f.initialIndentLevel + f.indentLevel - if n < 0 { - n = 0 - } + n := max(f.initialIndentLevel+f.indentLevel, 0) return f.eol + strings.Repeat(f.indentValue, n) } diff --git a/go.mod b/go.mod index 3a3c5ac..569b64c 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/sourcegraph/jsonx -go 1.25.9 +go 1.26.4 diff --git a/parser.go b/parser.go index 51a1be0..d4ae0ef 100644 --- a/parser.go +++ b/parser.go @@ -43,13 +43,13 @@ func ParseWithDetailedErrors(text string, options ParseOptions) ([]byte, []Parse valid bool } type parent struct { - array *[]interface{} - object map[string]interface{} + array *[]any + object map[string]any } - currentParent := parent{array: &[]interface{}{}} + currentParent := parent{array: &[]any{}} previousParents := []parent{} - onValue := func(value interface{}) { + onValue := func(value any) { if currentParent.array != nil { *currentParent.array = append(*currentParent.array, value) } else if currentProperty.valid { @@ -62,7 +62,7 @@ func ParseWithDetailedErrors(text string, options ParseOptions) ([]byte, []Parse var errors []ParseError visitor := Visitor{ OnObjectBegin: func(offset, length int) { - object := map[string]interface{}{} + object := map[string]any{} onValue(object) previousParents = append(previousParents, currentParent) currentParent = parent{object: object} @@ -78,7 +78,7 @@ func ParseWithDetailedErrors(text string, options ParseOptions) ([]byte, []Parse previousParents = previousParents[:len(previousParents)-1] }, OnArrayBegin: func(offset, length int) { - array := &[]interface{}{} + array := &[]any{} onValue(array) previousParents = append(previousParents, currentParent) currentParent = parent{array: array} @@ -89,7 +89,7 @@ func ParseWithDetailedErrors(text string, options ParseOptions) ([]byte, []Parse currentParent = previousParents[len(previousParents)-1] previousParents = previousParents[:len(previousParents)-1] }, - OnLiteralValue: func(value interface{}, offset, length int) { + OnLiteralValue: func(value any, offset, length int) { onValue(value) }, OnError: func(errorCode ParseErrorCode, offset, length int) { diff --git a/tree.go b/tree.go index 5cc24bc..7f1deeb 100644 --- a/tree.go +++ b/tree.go @@ -30,13 +30,13 @@ const ( // // Source: https://github.com/Microsoft/vscode/blob/c0bc1ace7ca3ce2d6b1aeb2bde9d1bb0f4b4bae6/src/vs/base/common/json.ts#L616 type Node struct { - Type NodeType // the node's type - Value interface{} // the node's value - Offset int // character offset of the node's starting position in the document - Length int // the length (in characters) of the node - ColumnOffset int // character offset of the property's separator - Parent *Node // the node's parent or nil if this is the root node - Children []*Node // the node's children + Type NodeType // the node's type + Value any // the node's value + Offset int // character offset of the node's starting position in the document + Length int // the length (in characters) of the node + ColumnOffset int // character offset of the property's separator + Parent *Node // the node's parent or nil if this is the root node + Children []*Node // the node's children } // A Segment is a component of a JSON key path. It is either an object @@ -64,7 +64,7 @@ func (s Segment) MarshalJSON() ([]byte, error) { // UnmarshalJSON implements json.Unmarshaler. func (s *Segment) UnmarshalJSON(data []byte) error { var t Segment - var target interface{} + var target any if len(data) > 0 && data[0] == '"' { t.IsProperty = true target = &t.Property @@ -98,7 +98,7 @@ func PropertyPath(properties ...string) Path { // each of which may be either a string (which is treated as a property segment) // or an int (which is treated as an array index). Any other type causes it // to panic. -func MakePath(components ...interface{}) Path { +func MakePath(components ...any) Path { segments := make([]Segment, len(components)) for i, c := range components { switch v := c.(type) { @@ -154,7 +154,7 @@ func ParseTree(text string, options ParseOptions) (root *Node, errors []ParseErr currentParent = currentParent.Parent ensurePropertyComplete(offset + length) }, - OnLiteralValue: func(value interface{}, offset, length int) { + OnLiteralValue: func(value any, offset, length int) { onValue(&Node{Type: literalNodeType(value), Offset: offset, Length: length, Parent: currentParent, Value: value}) ensurePropertyComplete(offset + length) }, @@ -180,7 +180,7 @@ func ParseTree(text string, options ParseOptions) (root *Node, errors []ParseErr return root, errors } -func literalNodeType(value interface{}) NodeType { +func literalNodeType(value any) NodeType { switch value.(type) { case bool: return Boolean @@ -254,17 +254,17 @@ func FindNodeAtLocation(root *Node, path Path) *Node { // NodeValue returns the JSON parse tree node's value. // // Source: https://github.com/Microsoft/vscode/blob/c0bc1ace7ca3ce2d6b1aeb2bde9d1bb0f4b4bae6/src/vs/base/common/json.ts#L782 -func NodeValue(node Node) interface{} { +func NodeValue(node Node) any { switch node.Type { case Array: - array := make([]interface{}, len(node.Children)) + array := make([]any, len(node.Children)) for i, child := range node.Children { array[i] = NodeValue(*child) } return array case Object: - object := make(map[string]interface{}, len(node.Children)) + object := make(map[string]any, len(node.Children)) for _, prop := range node.Children { object[prop.Children[0].Value.(string)] = NodeValue(*prop.Children[1]) } diff --git a/visitor.go b/visitor.go index a37e1e5..daf4468 100644 --- a/visitor.go +++ b/visitor.go @@ -34,7 +34,7 @@ type Visitor struct { // Invoked when a literal value is encountered. The offset and length represent // the location of the literal value. - OnLiteralValue func(value interface{}, offset, length int) + OnLiteralValue func(value any, offset, length int) // Invoked when a comma or colon separator is encountered. The offset and length // represent the location of the separator. @@ -105,7 +105,7 @@ func (w *walker) onArrayEnd() { } } -func (w *walker) onLiteralValue(value interface{}) { +func (w *walker) onLiteralValue(value any) { if w.visitor.OnLiteralValue != nil { w.visitor.OnLiteralValue(value, w.scanner.TokenOffset(), w.scanner.TokenLength()) }