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

CLI commands with object results print js-but-not-json #12

Open
sparr opened this issue Oct 3, 2017 · 18 comments
Open

CLI commands with object results print js-but-not-json #12

sparr opened this issue Oct 3, 2017 · 18 comments

Comments

@sparr
Copy link

sparr commented Oct 3, 2017

> storage.db['rooms.objects'].find({room: 'W1N7', type: 'controller'})
[ { room: 'W1N7',
    type: 'controller',
    x: 41,
    y: 31,
    level: 0,
    _id: 'fd2c0774d801520',
    meta: { revision: 0, created: 1477476568448, version: 0 },
    '$loki': 239 } ]

It would be a lot easier to have other programs interact with the CLI if this response was valid JSON instead of just valid JS. I think the only change would be to use double quotes, and to quote the keys.

This could be a problem with lokijs. I wasn't able to follow the behavior of the CLI code far enough to find out.

@RiftLurker
Copy link

I think it makes sense to return actual js-objects, as you might want to continue working with them inside the CLI.
If you want a JSON output, you can use JSON.stringify.

@TooAngel
Copy link

TooAngel commented Oct 4, 2017

The point here is, that you get a js object as a string back and need to parse (eval) it somehow to work with the data.
So transferring it over the socket as something parseable (JSON) would help to work with.

@sparr
Copy link
Author

sparr commented Oct 4, 2017

@PostCrafter returning actual js-objects would be an improvement. All we're seeing here is the stringification of the object. If it was in JSON, it would still be valid JS, but as things sit now it's valid JS without being valid JSON.

@AlinaNova21
Copy link
Contributor

I agree, the current JS objects are fine, if you need json then append .then(r=>JSON.stringify(r)) to the command. The CLI does not work well with external scripting and tools anyway. If you want that much control, a mod is much better.

@sparr
Copy link
Author

sparr commented Oct 4, 2017

@ags131 what does the current output gain us? Why would you want to stringify js objects in a way that isn't json?

@AlinaNova21
Copy link
Contributor

The CLI is not meant for scripting, its meant for human interaction and uses simple, human readable output. theres no need for JSON, rather than wrapping the CLI in tools, its much more effective to write a mod and interface directly with the DB (Or if using screepsmod-mongo, you can just interact with mongo/redis directly)

@sparr
Copy link
Author

sparr commented Oct 4, 2017

Would switching to JSON make it less human readable? We're just talking about putting quotes around strings.

@TooAngel
Copy link

TooAngel commented Oct 4, 2017

The advantage of using the CLI interface is having a standard way how to interact with the server independently on the storage backend.
The human readable is a point, but I don't think this should be considered on the network communication level. How to represent the data should be done on the client, the interface should be designed to be used by pieces of software which like parsable data ;-)

@sparr
Copy link
Author

sparr commented Oct 4, 2017

For my own edification, and because it might shed light on this dispute, can anyone pinpoint the code where the js object is getting turned into a string? I was unable to find it; the layout of the CLI code stumped me. The contents of that line and/or its location might add some perspective to this matter.

@AlinaNova21
Copy link
Contributor

As a counterpoint, I regularly use the CLI via telnet on machines without screeps installed, requiring a client to get the nicer output would remove that ease of use.

@AlinaNova21
Copy link
Contributor

@sparr
Copy link
Author

sparr commented Oct 4, 2017

I am at a loss for figuring out how that code works, where util.inspect comes from, or what callback that function is being called with.

Somewhere there is a block of code that turns a JS object into the string quoted in my original post here. I'd like to find that block.

@TooAngel
Copy link

TooAngel commented Oct 4, 2017

socket.write((isResult ? "< " : "") + data + "\r\n", 'utf8');

@sparr
Copy link
Author

sparr commented Oct 4, 2017

Oh, I see, util is a node library.

https://nodejs.org/api/util.html#util_util_inspect_object_options

@TooAngel
Copy link

TooAngel commented Oct 4, 2017

The util.inspect() method returns a string representation of object that is primarily useful for debugging. :-)

@sparr
Copy link
Author

sparr commented Oct 4, 2017

So, I guess my question here is... Why util.inspect() instead of JSON.stringify(), possibly with util.inspect in the replacer function to handle types that JSON.stringify doesn't handle?

@TheRealMaxion
Copy link

TheRealMaxion commented Oct 4, 2017

JSON.stringify errors on circular references and removes undefined properties

@sparr
Copy link
Author

sparr commented Oct 4, 2017

That's a depressingly good answer :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants