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
Introduce a -tables option. #45
Conversation
Can one of the admins verify this patch? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just a few nits
@@ -113,6 +115,14 @@ func main() { | |||
os.Exit(2) | |||
} | |||
|
|||
if *tbls != "" { | |||
err := tables.ParseAndUpdateJsonDefinitions(*tbls) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
push into if stmt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -0,0 +1,50 @@ | |||
/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go-style is lowercase: name this jsonparser.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, thanks, I wasn't sure because I saw there is a generateTables.go
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed.
return definitions, err | ||
} | ||
|
||
err = json.Unmarshal(data, &definitions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return this err on the next line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch: done.
func ParseAndUpdateJsonDefinitions(file string) error { | ||
definitions, err := ParseJsonDefinitions(file) | ||
if err == nil { | ||
OverrideTables(definitions.IsLabelArg, definitions.LabelBlacklist, definitions.IsSortableListArg, definitions.SortableBlacklist, definitions.SortableWhitelist) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error guards are more normal:
if err != nil {
return err
}
happy flow...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -0,0 +1,42 @@ | |||
/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name this jsonparser_test.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed (always fun on OS X).
@@ -39,6 +40,7 @@ var ( | |||
dflag = flag.Bool("d", false, "alias for -mode=diff") | |||
mode = flag.String("mode", "", "formatting mode: check, diff, or fix (default fix)") | |||
path = flag.String("path", "", "assume BUILD file has this path relative to the workspace directory") | |||
tbls = flag.String("tables", "", "path to JSON file with custom table definitions") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/tbls/tables/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I concede tbls
is a terrible name, but I put this up initially for two reasons:
- Because I'm importing
github.com/bazelbuild/buildifier/tables
above, I can't name ittables
, can I? - If I name it
tablesFlag
or something like that, then am I supposed to update all of the lines above so the=
signs line up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to tablesPath
and re-indented.
Jenkins, test this please. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, just one question on variable naming.
10d4284
to
d268493
Compare
@pmbethe09 All comments addressed. I hope it's OK that I squashed the commits. I assume that you'd rather just apply the final, resulting commit than merge in a small branch of intermediate steps? I realize different organizations have their own preferences (Facebook works this way, preferring rebases to merges). |
@@ -113,6 +115,11 @@ func main() { | |||
os.Exit(2) | |||
} | |||
|
|||
if *tablesPath != "" && tables.ParseAndUpdateJsonDefinitions(*tablesPath) != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
grab the error from ParseAndUpdateJsonDefinitions and add to the Printf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Note that this undoes the original change where you asked me to combine the two conditions into a single if
. I don't know if there's some clever way to assign err
in the if
, though perhaps even if that's possible, it's not considered good style, so I did the simple thing.
@@ -0,0 +1,52 @@ | |||
/* | |||
Copyright 2016 Google Inc. All Rights Reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2017
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -0,0 +1,42 @@ | |||
/* | |||
Copyright 2016 Google Inc. All Rights Reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2017
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
SortableWhitelist: map[string]bool{}, | ||
} | ||
if !reflect.DeepEqual(expected, definitions) { | ||
t.Error("structs not equal") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add structs to error message
t.Errorf("ParseJsonDefinitions() = %v; want %v", definitions, expected)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I didn't know about %v
. This is my first time writing Go, tbh.
d268493
to
d6a9044
Compare
@pmbethe09 OK, all comments addressed. |
Jenkins, test this please. |
@@ -113,6 +115,14 @@ func main() { | |||
os.Exit(2) | |||
} | |||
|
|||
if *tablesPath != "" { | |||
err := tables.ParseAndUpdateJsonDefinitions(*tablesPath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
push into if
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how that works. For example, this does not compile:
if *tablesPath != "" && (err := tables.ParseAndUpdateJsonDefinitions(*tablesPath)) != nil {
fmt.Fprintf(os.Stderr, "buildifier: failed to parse %s for -tables: %s\n", *tablesPath, err)
os.Exit(2)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The if below, not above.
if *tablesPath != "" {
if err := tables.ParseAndUpdateJsonDefinitions(*tablesPath); err != nil {
...
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO. By pushing into if, you create a clear scope of set and inspect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, cool, I didn't know about that syntax. Yes, I agree that's nicer. Fixed.
The `-tables` option makes it possible to specify a JSON file whose properties define arguments that will be passed to the `tables.OverrideTables()` function such that other projects can define their own constraints on argument formatting. This is valuable to Buck or Skylark users that define rules that need to be formatted in a way that is different from buildifier's defaults. Test Plan: * Added a test: `tables/jsonParser_test.go`. * Verified that `bazel test //...` succeeds. * Created a JSON config for Buck and verified the output of the following was what I expected when run from the Buck repo: ``` export BUILDIFIER_DIFF="diff -u" find . -name BUCK | xargs -I {} ../buildifier/bazel-bin/buildifier/buildifier -buildifier_disable=label -tables=buildifier.json -d {} ``` For this test, the contents of `buildifier.json` were: ``` { "IsLabelArg": { }, "LabelBlacklist": { }, "IsSortableListArg": { "deps": true, "exported_deps": true, "provided_deps": true, "srcs": true, "visibility": true }, "SortableBlacklist": { "genrule.srcs": true }, "SortableWhitelist": { } } ```
d6a9044
to
edd37b0
Compare
Awesome, thanks so much for the review, @pmbethe09! |
The
-tables
option makes it possible to specify a JSON file whoseproperties define arguments that will be passed to the
tables.OverrideTables()
function such that other projects can definetheir own constraints on argument formatting. This is valuable to
Buck or Skylark users that define rules that need to be formatted in a
way that is different from buildifier's defaults.
Test Plan:
tables/jsonParser_test.go
.bazel test //...
succeeds.following was what I expected when run from the Buck repo:
For this test, the contents of
buildifier.json
were: