Jeffail/gabs

Check existence

pjebs opened this issue · 3 comments

pjebs commented

This may be related to: #14

In your example:

value, ok = jsonParsed.Search("outter", "inner", "value1").Data().(float64)
// value == 10.0, ok == true

value, ok = jsonParsed.Path("does.not.exist").Data().(float64)
// value == 0.0, ok == false

The second example is actually ambiguous.
ok could be false either because the path does not exist OR because the data could not be type asserted to float64.

Is there another way to just determine if the path exists -- irrespective of the data type or whether the type assertion failed.

pjebs commented

I haven't checked code but perhaps this is it:
jsonParsed.Path("does.not.exist").Data() == nil

pjebs commented

If the above code works, then maybe a nice convenience method: jsonParsed.Path("does.not.exist").Exists()

Hey @pjebs, your examples work but I've checked in Exists() to make things simpler. However, it is still faster to do a select on .Data().(type), to save you the extra search.