Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected path behavior #128

Closed
PsychoLlama opened this issue Nov 19, 2015 · 6 comments
Closed

Unexpected path behavior #128

PsychoLlama opened this issue Nov 19, 2015 · 6 comments
Assignees

Comments

@PsychoLlama
Copy link
Contributor

If you pass a string into path, the decimal will break it into two separate path calls. This is not always the desired behavior, especially when considering floating point numbers:

Gun().get('example').path(0.123).set('data')

set is never called because the float is Stringed, and then it reads as path(0).path(123), where 0 is not defined, leaving the rest of the chain unfulfilled. In instances like this, there should at least be some log message that warns you of using floating point numbers.

@PsychoLlama
Copy link
Contributor Author

Preferably, there would be some way to escape the value so that decimals aren't split. Javascript's native implementation, object['string value'], would likely be the most intuitive for users, and might look something like this...

gun.path("[0.123]")

Maybe we could have floats coerce into a string like that under the hood, too...

@PsychoLlama
Copy link
Contributor Author

Note to onlookers: this is a rough summary of a conversation held about what gun should and should not do.

@thoka
Copy link
Contributor

thoka commented Nov 19, 2015

One could pass all keys through https://github.com/deanlandolt/bytewise by default,
which would allow arbitrary complex keys.

@metasean metasean removed the doc label Nov 19, 2015
@metasean metasean assigned amark and unassigned metasean Nov 19, 2015
@metasean
Copy link
Collaborator

@thoka - Thank you so much for all of your pointers and suggestions!

I've added a "Potentially Unexpected Behavior" section to the path documentation and @amark will also be adding a console notification.

The current implementation is the way we want the core to work, but we would love some extensions that provide alternative approaches for gun users. Until extensions are coded, our goal is to ensure users know why the system is behaving the way it is.

@PsychoLlama
Copy link
Contributor Author

PsychoLlama commented May 10, 2016

From a conversation with @amark

There is a potentially better solution here, where we normalize everything to an array. If it's not an array and a string, then we split it on periods, turning it into an array. This would mean that only strings are split, preventing the odd behavior with floats or other data types. By consequence, you could have a bracket like syntax by passing an array instead of a string. Since it's wrapped in an array, no implicit string splitting is done, finally allowing you to use dots in your path names.

Normal Javascript

var obj = {
  '10.15.20': 'data'
}
obj['10.15.20'] // 'data'

Gun syntax

var obj = gun.get('obj')
// brackety syntax
obj.path(['10.15.20']) // path('10.15.20')

Whereas if we wanted to split on the period, we could explicitly pass a single string:

// or the usual string split
obj.path('10.15.20') // path(10).path(15).path(20)

Here's the code that could enable that:

var gun = this;

// if it's a string, not an array...
if (typeof input === 'string') {
    // split on dot notation
    input = input.split('.');
}
// otherwise it's not a string,
// but not an array either
if (!(input instanceof Array)) {
    // turn it into an array
    input = [input];
}
// if there's more than one item in
// the array, then call `.path` on each item.
if (input.length > 1) {
    input.forEach(function (path) {
        gun = gun.path([path]);
    });
    return gun;
}
// do normal `.path` things below

I'd really like to see something like this in the next release of gun 😀

@amark
Copy link
Owner

amark commented Feb 20, 2017

Path has now become a wrapper for get so get won't cause these problems anymore. It is note in the documentation about path though. And I think Jesse had added several patches fixing it. So closing!

@amark amark closed this as completed Feb 20, 2017
@amark amark removed the InProgress label Feb 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants