Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	go.mod
  • Loading branch information
elv-gilles committed Dec 17, 2024
2 parents ed9469c + e98bb04 commit fe5c492
Show file tree
Hide file tree
Showing 63 changed files with 10,343 additions and 1,394 deletions.
15 changes: 6 additions & 9 deletions .github/workflows/test.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: 1.21.x
go-version: 1.23.x
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Test
run: go test ./...
- name: TestInline
Expand All @@ -21,20 +21,17 @@ jobs:
run: diff -u <(echo -n) <(gofmt -s -d .)
# Test on a large matrix of Go versions and operating systems.
test-all:
# TODO: GitHub actions are not free on private repositories.
# When this is made public, remove this conditional.
if: github.repository == 'go-json-experiment/json'
strategy:
matrix:
go-version: [1.21.x]
go-version: [1.23.x]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Test
run: go test ./...
5 changes: 3 additions & 2 deletions arshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ func UnmarshalDecode(in *jsontext.Decoder, out any, opts ...Options) (err error)
return unmarshalDecode(in, out, uo)
}

var errNonNilReference = errors.New("value must be passed as a non-nil pointer reference")

func unmarshalDecode(in *jsontext.Decoder, out any, uo *jsonopts.Struct) (err error) {
v := reflect.ValueOf(out)
if !v.IsValid() || v.Kind() != reflect.Pointer || v.IsNil() {
Expand All @@ -438,8 +440,7 @@ func unmarshalDecode(in *jsontext.Decoder, out any, uo *jsonopts.Struct) (err er
t = t.Elem()
}
}
err := errors.New("value must be passed as a non-nil pointer reference")
return &SemanticError{action: "unmarshal", GoType: t, Err: err}
return &SemanticError{action: "unmarshal", GoType: t, Err: errNonNilReference}
}
va := addressableValue{v.Elem()} // dereferenced pointer is always addressable
t := va.Type()
Expand Down
23 changes: 14 additions & 9 deletions arshal_any.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"reflect"
"strconv"

"github.com/go-json-experiment/json/internal"
"github.com/go-json-experiment/json/internal/jsonflags"
"github.com/go-json-experiment/json/internal/jsonopts"
"github.com/go-json-experiment/json/internal/jsonwire"
Expand Down Expand Up @@ -71,9 +72,12 @@ func unmarshalValueAny(dec *jsontext.Decoder, uo *jsonopts.Struct) (any, error)
}
return makeString(xd.StringCache, val), nil
case '0':
if uo.Flags.Get(jsonflags.UnmarshalAnyWithRawNumber) {
return internal.RawNumberOf(val), nil
}
fv, ok := jsonwire.ParseFloat(val, 64)
if !ok && uo.Flags.Get(jsonflags.RejectFloatOverflow) {
return nil, &SemanticError{action: "unmarshal", JSONKind: k, GoType: float64Type, Err: strconv.ErrRange}
return nil, newUnmarshalErrorAfter(dec, float64Type, strconv.ErrRange)
}
return fv, nil
default:
Expand All @@ -88,7 +92,7 @@ func marshalObjectAny(enc *jsontext.Encoder, obj map[string]any, mo *jsonopts.St
if xe.Tokens.Depth() > startDetectingCyclesAfter {
v := reflect.ValueOf(obj)
if err := visitPointer(&xe.SeenPointers, v); err != nil {
return err
return newMarshalErrorBefore(enc, anyType, err)
}
defer leavePointer(&xe.SeenPointers, v)
}
Expand All @@ -99,7 +103,7 @@ func marshalObjectAny(enc *jsontext.Encoder, obj map[string]any, mo *jsonopts.St
return enc.WriteToken(jsontext.Null)
}
// Optimize for marshaling an empty map without any preceding whitespace.
if !xe.Flags.Get(jsonflags.Expand) && !xe.Tokens.Last.NeedObjectName() {
if !xe.Flags.Get(jsonflags.AnyWhitespace) && !xe.Tokens.Last.NeedObjectName() {
xe.Buf = append(xe.Tokens.MayAppendDelim(xe.Buf, '{'), "{}"...)
xe.Tokens.Last.Increment()
if xe.NeedFlush() {
Expand Down Expand Up @@ -176,8 +180,9 @@ func unmarshalObjectAny(dec *jsontext.Decoder, uo *jsonopts.Struct) (map[string]

// Manually check for duplicate names.
if _, ok := obj[name]; ok {
name := xd.PreviousBuffer()
err := export.NewDuplicateNameError(name, dec.InputOffset()-len64(name))
// TODO: Unread the object name.
name := xd.PreviousTokenOrValue()
err := newDuplicateNameError(dec.StackPointer(), nil, dec.InputOffset()-len64(name))
return obj, err
}

Expand All @@ -192,7 +197,7 @@ func unmarshalObjectAny(dec *jsontext.Decoder, uo *jsonopts.Struct) (map[string]
}
return obj, nil
}
return nil, &SemanticError{action: "unmarshal", JSONKind: k, GoType: mapStringAnyType}
return nil, newUnmarshalErrorAfter(dec, mapStringAnyType, nil)
}

func marshalArrayAny(enc *jsontext.Encoder, arr []any, mo *jsonopts.Struct) error {
Expand All @@ -201,7 +206,7 @@ func marshalArrayAny(enc *jsontext.Encoder, arr []any, mo *jsonopts.Struct) erro
if xe.Tokens.Depth() > startDetectingCyclesAfter {
v := reflect.ValueOf(arr)
if err := visitPointer(&xe.SeenPointers, v); err != nil {
return err
return newMarshalErrorBefore(enc, sliceAnyType, err)
}
defer leavePointer(&xe.SeenPointers, v)
}
Expand All @@ -212,7 +217,7 @@ func marshalArrayAny(enc *jsontext.Encoder, arr []any, mo *jsonopts.Struct) erro
return enc.WriteToken(jsontext.Null)
}
// Optimize for marshaling an empty slice without any preceding whitespace.
if !xe.Flags.Get(jsonflags.Expand) && !xe.Tokens.Last.NeedObjectName() {
if !xe.Flags.Get(jsonflags.AnyWhitespace) && !xe.Tokens.Last.NeedObjectName() {
xe.Buf = append(xe.Tokens.MayAppendDelim(xe.Buf, '['), "[]"...)
xe.Tokens.Last.Increment()
if xe.NeedFlush() {
Expand Down Expand Up @@ -259,5 +264,5 @@ func unmarshalArrayAny(dec *jsontext.Decoder, uo *jsonopts.Struct) ([]any, error
}
return arr, nil
}
return nil, &SemanticError{action: "unmarshal", JSONKind: k, GoType: sliceAnyType}
return nil, newUnmarshalErrorAfter(dec, sliceAnyType, nil)
}
Loading

0 comments on commit fe5c492

Please sign in to comment.