Skip to content

Silence warnings for unknown top-level fields starting with an underscore#146

Merged
sol merged 3 commits intosol:masterfrom
liskin:ign-underscored-fields
Jan 24, 2017
Merged

Silence warnings for unknown top-level fields starting with an underscore#146
sol merged 3 commits intosol:masterfrom
liskin:ign-underscored-fields

Conversation

@liskin
Copy link
Copy Markdown
Collaborator

@liskin liskin commented Jan 12, 2017

This lets users declare standalone YAML anchors and reuse them using YAML
aliases later:

_: &mydeps
 - base
 - containers
_my_library_of_hpack_stuff:
 - &mydeps [base, containers]
 - &myflags [-threaded, -Wall]

Also, this pull request adds a test that field overriding works as
expected and extends the README with a section on not repeating
yourself using these YAML tricks.

See also #144, which is a followup (logically, but not chronologically)
to this and enables putting those _libraries_of_hpack_stuff to separate
files and including them in multiple package.yamls across a larger
codebase.

README.md Outdated
with an underscore, so you can declare global aliases too:

```yaml
_: &exe-ghc-options
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Let's use a variation of the common convention where the field name and anchor name are the same:

_exe-ghc-options: &exe-ghc-options
  - ...

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

My main motivation here is that if the user uses _ multiple times we rely on the non-standard libyaml behavior. This is why I prefer an example that avoids this problem by including the reference name in the field name.

name: n1
name: n2
|]
(packageName >>> (`shouldBe` "n2"))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Now that we ignore warnings for fields that start with an underscore, is this still critical? I think it's better not to rely on that behavior. Please remove this test if you agree.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It is still critical because otherwise merge and override (d10a66d#diff-04c6e90faac2675aa89e2176d2eec7d8R194) wouldn't work.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

My assumption was that this will always work and is not violating the spec. But then again, I'm not sure.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'm not really sure how to interpret the spec regarding this, but I think that testing for the merge&override case specifically instead of this double definition would be a good idea anyway. :-)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Yes, I'm ok with that.

fields = fieldNames (Proxy :: Proxy a)
ignoreUnderscored
| warnUnderscoredUnknownFields (Proxy :: Proxy a) = id
| otherwise = filter (not . isPrefixOf "_")
Copy link
Copy Markdown
Owner

@sol sol Jan 22, 2017

Choose a reason for hiding this comment

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

I like your code, it's quite elegant and extensible if we ever want to change the behavior in the future. Yet, being an advocate of TDD, I usually tend to go with the simplest solution that solves the problem and defer more clever solutions until we actually need them.

Further down we have a line

mkPackage dir (CaptureUnknownFields unknownFields globalOptions@Section{sectionData = PackageConfig{..}}) = do

Here you could make the changes with a two liner (using view patterns):

ignoreUnderscored = filter (not . isPrefixOf "_")

mkPackage dir (CaptureUnknownFields (ignoreUnderscored -> unknownFields) globalOptions@Section{sectionData = PackageConfig{..}}) = do

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Oh, I had almost the exact same code but then I realized we agreed on top-level unknown fields and rewrote it this way. :-(
But sure, I can simplify this.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think the code I suggested will behave exactly the same way as your code (that is only ignore top-level fields that start with an underscore).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It won't, it will ignore this: https://github.com/sol/hpack/pull/146/files/d10a66db70aff2c225622fcdda929d15d92f7312#diff-af92d09f3a5eb3bf28df295b3a1fa13fR327 as well and I didn't consider that a top-level field. But I really don't have any strong feelings about this. If you think the simplification is worth it, let's do it that way. :-)

Copy link
Copy Markdown
Owner

@sol sol Jan 23, 2017

Choose a reason for hiding this comment

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

Ah, I see, it's because we treat unknown fields from top-level conditionals the same way as unknown top-level fields. In that case let's go with your solution.

Given that, there is only one more thing I would ask. Can we rename warnUnderscoredUnknownFields to ignoreUnderscoredUnknownFields and inverse the bools. That code is not really concerned with warnings, but rather with collecting unknown field names. Later we decide to turn them into warnings, but we could as well turn them into e.g. errors at some later point.

|]
(`shouldBe` [
"Ignoring unknown field \"bar\" in package description"
"Ignoring unknown field \"_qux\" in package description"
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

If you go with the two-liner solution this test case becomes redundant.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

As discussed we actually want to keep this test case.

…core

This lets users declare standalone YAML anchors and reuse them using
YAML aliases later:

```yaml
_mydeps: &mydeps
  - base
  - containers
```

or

```yaml
_my_library_of_hpack_stuff:
  - &mydeps [base, containers]
  - &myflags [-threaded, -Wall]
```
Before documenting how YAML can be abused to define common fields and
reference them elsewhere, possibly overriding some, we should test that
Data.Yaml actually behaves the way we need (that is, inversely to what
YAML 1.1 spec says: http://yaml.org/spec/1.1/#mapping/syntax).
@liskin liskin force-pushed the ign-underscored-fields branch from d10a66d to 309d9f4 Compare January 23, 2017 16:49
@liskin
Copy link
Copy Markdown
Collaborator Author

liskin commented Jan 23, 2017

I pushed an updated version that hopefully addresses everything we agreed upon.

@sol sol merged commit b106969 into sol:master Jan 24, 2017
@liskin liskin deleted the ign-underscored-fields branch January 24, 2017 14:55
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.

2 participants