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

Assume config value to be json-formatted in cli #1355

Closed
wants to merge 2 commits into from

Conversation

rht
Copy link
Contributor

@rht rht commented Jun 11, 2015

This deprecates explicit '--bool' and '--json' flag.
The reason is, if explicit type has to be specified, then '--integer',
'--float' also need to be implemented. But json parser already knows how
to handle these types, so why not just reuse it.
ref: #740

@jbenet jbenet added the backlog label Jun 11, 2015
@GitCop
Copy link

GitCop commented Jun 11, 2015

There were the following issues with your Pull Request

  • Commit: 6c33579
    • Invalid signoff. Commit message must end with License: MIT
      Signed-off-by: .* <.*>

Guidelines are available at: https://github.com/ipfs/community/blob/master/docs/commit-message.md Your feedback on GitCop is welcome on the following issue: ipfs/infra#23


This message was auto-generated by https://gitcop.com

rht and others added 2 commits June 11, 2015 21:41
License: MIT
Signed-off-by: rht <rhtbot@gmail.com>
This deprecates explicit '--bool' and '--json' flag.
The reason is, if explicit type has to be specified, then '--integer',
'--float' also need to be implemented. But json parser already knows how
to handle these types, so why not just use it.

License: MIT
Signed-off-by: rht <rhtbot@gmail.com>
@whyrusleeping
Copy link
Member

we should re-run the tests here. everything appears to have failed

if err := json.Unmarshal([]byte(value), &jsonVal); err != nil {
err = fmt.Errorf("failed to unmarshal json. %s", err)
res.SetError(err, cmds.ErrNormal)
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so always default to json? that makes sense, but does that mean that we now have to do:

# used to work
> ipfs config Addresses.API /ip4/127.0.0.1/tcp/5001

# must now be
> ipfs config Addresses.API "/ip4/127.0.0.1/tcp/5001"

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Thu, Jun 11, 2015 at 01:34:07PM -0700, Juan Batiz-Benet wrote:

must now be

ipfs config Addresses.API "/ip4/127.0.0.1/tcp/5001"

Implementations differ in how strictly they enforce this, but I've
seen a number of folks suggest that strings are not valid JSON root
objects (e.g. 1). That's also how it's written up in RFC 4627 2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ipfs config Addresses.API /ip4/127.0.0.1/tcp/5001 still works in this case (golang/pkg/encoding/json).

So I will have to check which implementation (RFC 4627 vs ECMA-262 5 and newer) is among the majority today.

edit: from here on 'ECMA-262 5' is renamed RFC 7159 https://tools.ietf.org/html/rfc7159#section-2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ipfs config Addresses.API /ip4/127.0.0.1/tcp/5001 still works

ok great.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accept stand-alone strings, numbers (RFC 7159):

  • Go 1.3+
  • Major browsers (IE 8+, everything else not IE)[1]
  • Python 2.7+[1]

else (RFC 4627):

  • Ruby 2.1.1 stdlib "json" [2]

Pending:
http://json.org/ (150-ish of those)

[1] http://stackoverflow.com/questions/18202532/can-a-single-stand-alone-literal-form-a-valid-json-document
[2] manual check & "JSON.generate only allows objects or arrays to be converted to JSON syntax." in http://ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html


Or since RFC 7159 supersedes RFC 4627, any legacy system will move toward it anyway.

@rht rht changed the title Assume config value to be json-formatted Assume config value to be json-formatted in cli Jun 12, 2015
@jbenet
Copy link
Member

jbenet commented Jun 12, 2015

I re-ran the tests. something bad's going on.

@rht
Copy link
Contributor Author

rht commented Jun 12, 2015

I see, I likely didn't type the beginning slash when testing ipfs config Addresses.API /ip4/127.0.0.1/tcp/5001.

With this commit, all strings must be explicitly formatted as "string".

I suppose the options now are:

  1. (Early version of this PR rht@f38c58d) Everything is json.
    • It's tedious to make sure to surround strings with "'s.
    • Breaks backward compatibility with env vars stored in dotfiles (...suppose this is not an issue).
    • Every type is on equal footing.
    • No need for "maybe use --json?"
  2. Default to string. Keep the flags as they are currently, then just add '--number'. Where '--bool' and '--number' are actually '--json' in disguise.
    • ipfs config foo 137 doesn't get parsed as number.
    • much longer than option 3
  3. Default to string. Use '--json' for everything else (object, array, number, bool, null).
    • ipfs config foo 137 doesn't get parsed as number.
    • Very roughly 75% of the time '--json' is required.
  4. Default to json. If fails to unmarshall, then just set as string.
    • Mixes valid string and malformed non-strings.
    • No need for "maybe use --json?".

@jbenet
Copy link
Member

jbenet commented Jun 13, 2015

  • (1.) normally, i'd like this to be the end state, but this breaks user space. :( not so sure.
  • (2.) -1 dont like this one.
  • (3.) I think i like this one. and deprecate --bool. (don't remove it yet, but later)
  • (4.) is a bit concerning, its the type of thing that backfires annoyingly. It may be ok we also have --json and --string just in case the auto-detect fails.

@whyrusleeping
Copy link
Member

maybe we can be a little smart about things. things like: if strconv.Atoi doesnt return an error, we can assume its a number

@jbenet
Copy link
Member

jbenet commented Jun 14, 2015

I'd prefer being simple and explicit. Smartness can introduce weird edge cases

@rht
Copy link
Contributor Author

rht commented Jun 14, 2015

I think strconv.Atoi is a valid literal type inference. Will this be extended to Atob, Atof, json.Unmarshal (for list and object)... then fallback to string? If so this is basically (4.).

  • Default to string? (2, 3)
    I have to amend one point in (3.): actually string is still the >50% most common value in the config (the reason why default to string is the current state in the first place). But other types are still common.
  • Default to json? (1, 4)
    There is no need to "maintain '--bool' backward compatibility", since bools/numbers are automatically parsed. One can still specify '--bool'/'--number' but this is placebo.

The only breaking change in (1.) is the necessity of "--string".
It's just that you rarely see this in other configuration languages. But you also rarely see explicit type annotation like '--bool', '--number'.

(4.) by itself blurs the edge cases, i.e. it emulates the flexibility "smartness" of a human parser, but also its sloppiness. But this can still be fine because the config struct's are explicitly typed. And so this will always detect a malformed bool/int/float/array/object as a final check. This 2nd type check doesn't exist for user-specified values, however (and if this is hard to debug...). Also doesn't exist in dynamically typed languages.

There is actually option (5.): use https://github.com/vstakhov/libucl (as a superset of json) just for parsing then serialize to json.

There is no requirement of quotes for strings and keys, ...

It looks like strings can be inferred unambiguously and efficiently. But this is not a sufficient reason to integrate the whole library though (and also more formats to keep track of for compatibility: json, ucl, go types, other ipfs implementation types).

@jbenet
Copy link
Member

jbenet commented Jun 30, 2015

@rht not sure here. i think we should make sure not to

  • break current user scripts (hence keep the --bool flag for now even if it doesnt do anything different)
  • make sure not to get into weird edge cases.
  • allow explicitness (make sure to keep --json which will only parse json or fail)

if you want to try (4.) then let's do it, but we'll need good tests to make sure various things get set correctly to what we expect.

@rht
Copy link
Contributor Author

rht commented Jul 2, 2015

The only edge case with (4) is with user-specified values.
Everything else is unchanged.

The commands.Option object actually has a very similar use case.
In this case, the choice is (4) + value auto-cast (https://github.com/ipfs/go-ipfs/blob/master/commands/request.go#L224-L267), which makes sense because there won't be any user-specified command options.

@rht rht closed this Jul 2, 2015
@jbenet jbenet removed the backlog label Jul 2, 2015
@rht rht deleted the cleanup-cli branch July 2, 2015 15:43
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

Successfully merging this pull request may close these issues.

5 participants