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

Switch to snake_case by convention for values #518

Merged
merged 4 commits into from Dec 29, 2021
Merged

Switch to snake_case by convention for values #518

merged 4 commits into from Dec 29, 2021

Conversation

mboes
Copy link
Member

@mboes mboes commented Dec 28, 2021

This renames all builtins and standard library functions to conform to
a snake case convention, as per decision recorded in #493. This also
adapts examples and tests to this change. All functions have been
renamed the way you'd expect, except the following:

  • fromPred -> from_predicate
  • typeOf -> typeof (like in C)
  • fieldsOf -> fields (the _of is redundant for a function)
  • valuesOf -> values (the _of is redundant for a function)

Closes #493

This renames all builtins and standard library functions to conform to
a snake case convention, as per decision recorded in #493. This also
adapts examples and tests to this change. All functions have been
renamed the way you'd expect, except the following:

* `fromPred` -> `from_predicate`
* `typeOf` -> `typeof` (like in C)
* `fieldsOf` -> `fields` (the `_of` is redundant for a function)
* `valuesOf` -> `values` (the `_of` is redundant for a function)

Closes #493
@github-actions github-actions bot temporarily deployed to pull request December 28, 2021 21:54 Inactive
@mboes mboes requested a review from yannham December 28, 2021 22:41
Copy link
Member

@yannham yannham left a comment

Choose a reason for hiding this comment

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

LGTM overall, but the fields exported in a configuration should not all be automatically capitalized in snake_case in the examples, I think.

@@ -51,7 +51,7 @@ let OptLevel = fun label value =>
contracts.blame label in

let Contract = {
pathLibC | doc "Path to libc."
path_libc | doc "Path to libc."
Copy link
Member

Choose a reason for hiding this comment

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

Should we apply the change to configuration option? In real life, their name is dictated by the consuming tool (say, Terraform), most commonly in camelCase than in snake_case.

@@ -11,7 +11,7 @@ let PortElt
| doc "A contract for a port element of a Kubernetes configuration"
= {
name | Str,
containerPort | #Port,
container_port | #Port,
Copy link
Member

@yannham yannham Dec 29, 2021

Choose a reason for hiding this comment

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

Illustrating the previous comment, the field names in this example are taken from an actual Kubernetes schema, so we don't have the freedom of picking the case.

Copy link
Member Author

Choose a reason for hiding this comment

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

I might have been trigger happy here. But again, I think it's best to have an explicit pointer to the schema, otherwise future folks like me won't know that the names were not picked freely. Do you have a URL handy?

Copy link
Member

Choose a reason for hiding this comment

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

The official reference is https://kubernetes.io/docs/reference/kubernetes-api/. Another useful and better tailored resource is the original example it was inspired from (IIRC): https://github.com/kubernetes/examples/blob/master/guestbook-go/guestbook-controller.json.


typeOf : Dyn -> <
typeof : Dyn -> <
Copy link
Member

Choose a reason for hiding this comment

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

Should we really follow C/C++ precedent here, though? For example, this random Rust crate uses type_of, which IMHO is more consistent with our and Rust's naming convention.

Copy link
Member Author

Choose a reason for hiding this comment

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

The value of any foo applied to baz is the foo of baz. So any _of in a function is redundant and shouldn't be part of any naming convention. The only reason something like it is there in C/C++ and elsewhere is presumably because type is a reserved keyword. I saw the "random Rust crate" you mention, but with 480 downloads over its lifetime, it hardly has enough clout to set a meaningful precedent.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, fair enough. By the way type is currently not a reserved keyword in Nickel, and we've been trying to avoid to do so when possible (such as with type aliases) as it may easily appear in configurations and would be annoying to escape everywhere (e.g. in the current schema of NixOS modules). However it is not be good idea to tie our hands now and prevent any future use of a type keyword, so let's go with typeof.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thinking about this more: in C/C++, typeof is an operator. Probably has that name because no other keyword contains an underscore. In Python, the equivalent is a function called type(). Could we do the same here? type doesn't appear to be a reserved keyword yet and maybe we never want it to be. I have provisionally updated the PR accordingly.

Copy link
Member Author

@mboes mboes Dec 29, 2021

Choose a reason for hiding this comment

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

Hm, don't know what to make of the message timestamps above. You somehow answered my question from 14 minutes in the future. :)

I've kept typeof then. It's easier to go typeof -> type later, than the other way around.

Copy link
Member

Choose a reason for hiding this comment

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

I actually answered from the future indeed 😛 I wrote my reply as an answer to your first comment without seeing the following ones.

Copy link
Member Author

@mboes mboes left a comment

Choose a reason for hiding this comment

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

the fields exported in a configuration should not all be automatically capitalized in snake_case in the examples, I think.

I agree that configuration should use whatever schema is imposed by upstream. But in that case we should be explicitly referencing what schema we are assuming. API fields are no more commonly camelCase than snake_case IME. It really depends.

@@ -11,7 +11,7 @@ let PortElt
| doc "A contract for a port element of a Kubernetes configuration"
= {
name | Str,
containerPort | #Port,
container_port | #Port,
Copy link
Member Author

Choose a reason for hiding this comment

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

I might have been trigger happy here. But again, I think it's best to have an explicit pointer to the schema, otherwise future folks like me won't know that the names were not picked freely. Do you have a URL handy?


typeOf : Dyn -> <
typeof : Dyn -> <
Copy link
Member Author

Choose a reason for hiding this comment

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

The value of any foo applied to baz is the foo of baz. So any _of in a function is redundant and shouldn't be part of any naming convention. The only reason something like it is there in C/C++ and elsewhere is presumably because type is a reserved keyword. I saw the "random Rust crate" you mention, but with 480 downloads over its lifetime, it hardly has enough clout to set a meaningful precedent.

Add a pointer to original example.
@mboes
Copy link
Member Author

mboes commented Dec 29, 2021

@yannham PTAL

@github-actions github-actions bot temporarily deployed to pull request December 29, 2021 16:31 Inactive
@github-actions github-actions bot temporarily deployed to pull request December 29, 2021 16:44 Inactive
Copy link
Member

@yannham yannham left a comment

Choose a reason for hiding this comment

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

Beside two leftover in the Kubernetes example, it's good to go.

examples/record-contract/record-contract.ncl Outdated Show resolved Hide resolved
examples/record-contract/record-contract.ncl Outdated Show resolved Hide resolved
@github-actions github-actions bot temporarily deployed to pull request December 29, 2021 18:55 Inactive
@github-actions github-actions bot temporarily deployed to pull request December 29, 2021 19:21 Inactive
@mboes mboes merged commit 033ed33 into master Dec 29, 2021
@mboes mboes deleted the snake_case branch December 29, 2021 19:30
mboes added a commit that referenced this pull request Dec 30, 2021
In #518, I had forgotten to change the names of the primitives in
`stdlib/strings.ncl`. I noticed the omission because this was breaking
the example in the nickel-lang.org playground.
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.

Case convention: use snake_case for values
2 participants