ohler55 / ojg

Optimized JSON for Go
MIT License
839 stars 50 forks source link

Zero values excluded from results #83

Closed gregschrock closed 2 years ago

gregschrock commented 2 years ago

Description

Zero values are being excluded from an expression's Get results. It seems this comes down to this line. However, this will also be the case for a few other code paths. The result is incorrect according to JSONPath.

https://github.com/ohler55/ojg/blob/ab7f7e6304d3f9eb098606950d207dede590c6ae/jp/get.go#L1151

I'm wondering if this is intentional or maybe it could be optional somehow. As it stands, the library isn't suitable for a use case it's otherwise perfect for. Thanks for your contribution to the community!

Reproduction

package test

import (
    "testing"

    "github.com/ohler55/ojg/jp"
    "github.com/stretchr/testify/assert"
)

type Bool struct {
    Value bool
}

func TestFalseValues(t *testing.T) {
    trueBool := Bool{true}
    falseBool := Bool{false}

    exp, err := jp.ParseString(`$.Value`)
    if err != nil {
        assert.NoError(t, err)
        return
    }

    trueResults := exp.Get(trueBool)
    assert.Len(t, trueResults, 1)
    if len(trueResults) == 1 {
        assert.Equal(t, true, trueResults[0])
    }

    falseResults := exp.Get(falseBool)
    assert.Len(t, falseResults, 1)
    if len(falseResults) == 1 {
        assert.Equal(t, false, falseResults[0])
    }
}

Expected

ok

Actual

--- FAIL: TestFalseValues (0.00s)
    simple_test.go:32: 
            Error Trace:    simple_test.go:31
            Error:          "[]" should have 1 item(s), but has 0
            Test:           TestFalseValues
FAIL
FAIL    simple_test 0.010s
FAIL
ohler55 commented 2 years ago

I see your point. Let me look at the best fix.

ohler55 commented 2 years ago

Logically, you are correct. I don't know what I was thinking with that check. Please try the "has-is-not-zero" branch.

gregschrock commented 2 years ago

Thanks for the quick turn around! I got a chance to test against those changes and it does now function as I expect.

ohler55 commented 2 years ago

Great, I'll make a release then.

ohler55 commented 2 years ago

Released

gregschrock commented 2 years ago

Addressed by v1.12.13