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

Introduce a -tables option. #45

Merged
merged 1 commit into from Jan 30, 2017
Merged

Conversation

bolinfest
Copy link
Contributor

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": {
  }
}

@bazel-io
Copy link
Member

Can one of the admins verify this patch?

Copy link
Member

@pmbethe09 pmbethe09 left a 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)
Copy link
Member

Choose a reason for hiding this comment

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

push into if stmt

Copy link
Contributor Author

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 @@
/*
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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)
Copy link
Member

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

Copy link
Contributor Author

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)
Copy link
Member

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...

Copy link
Contributor Author

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 @@
/*
Copy link
Member

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

Copy link
Contributor Author

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")
Copy link
Member

Choose a reason for hiding this comment

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

s/tbls/tables/

Copy link
Contributor Author

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:

  1. Because I'm importing github.com/bazelbuild/buildifier/tables above, I can't name it tables, can I?
  2. 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?

Copy link
Contributor Author

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.

@pmbethe09
Copy link
Member

Jenkins, test this please.

Copy link
Contributor Author

@bolinfest bolinfest left a 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.

@bolinfest bolinfest force-pushed the custom-tables branch 2 times, most recently from 10d4284 to d268493 Compare January 30, 2017 18:15
@bolinfest
Copy link
Contributor Author

@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 {
Copy link
Member

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

Copy link
Contributor Author

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.
Copy link
Member

Choose a reason for hiding this comment

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

2017

Copy link
Contributor Author

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.
Copy link
Member

Choose a reason for hiding this comment

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

2017

Copy link
Contributor Author

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")
Copy link
Member

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)

Copy link
Contributor Author

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.

@bolinfest
Copy link
Contributor Author

@pmbethe09 OK, all comments addressed.

@pmbethe09
Copy link
Member

Jenkins, test this please.

@@ -113,6 +115,14 @@ func main() {
os.Exit(2)
}

if *tablesPath != "" {
err := tables.ParseAndUpdateJsonDefinitions(*tablesPath)
Copy link
Member

Choose a reason for hiding this comment

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

push into if

Copy link
Contributor Author

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)
	}

Copy link
Member

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 {
...
}
}

Copy link
Member

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.

Copy link
Contributor Author

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": {
  }
}
```
@pmbethe09 pmbethe09 merged commit 451dae5 into bazelbuild:master Jan 30, 2017
@bolinfest
Copy link
Contributor Author

Awesome, thanks so much for the review, @pmbethe09!

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.

None yet

4 participants