tidwall / tidwall/gjson

Result.Exists() has incorrect CMP and probably returns incorrect result

Open
#166 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
15.6k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

In source code

// Exists returns true if value exists.
//
//  if gjson.Get(json, "name.last").Exists(){
//		println("value exists")
//  }
func (t Result) Exists() bool {
	return t.Type != Null || len(t.Raw) != 0
}

The OR operator should probably be AND. Otherwise cmp with Null doesn't add any logic.

The expression can be written as

func (t Result) Exists() bool {
	return t.Raw !="null" || len(t.Raw) != 0
}

where len(t.Raw) != 0 is also true for t.Raw =="null", so first check does nothing

However, if op will be AND, the condition will be false for nulls and missing values.

I don't know which behaviour is supposed to be here, but to make code cleaner, one of these should be used

  • return len(t.Raw) != 0 or
  • return t.Type != Null && len(t.Raw) != 0

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Result.Exists method shown in the issue and inspect how Result.Type, Result.Raw, Null, and missing values are represented elsewhere in the source. Determine the intended behavior for null and missing results, then update the condition and add or adjust coverage if existing tests exercise Exists.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.