go-json-experiment / json

Experimental implementation of a proposed v2 encoding/json package
BSD 3-Clause "New" or "Revised" License
341 stars 11 forks source link

Use constructors for static tokens #29

Open dsnet opened 2 months ago

dsnet commented 2 months ago

WARNING: This commit includes breaking changes.

Instead of global variables, use function constructors for static tokens such as Null, ObjectStart, ObjectEnd, ArrayStart, and ArrayEnd. This provides a greater degree of immutability for the API.

Note that static tokens for True and False are dropped in favor of the pre-existing Bool constructor. If necessary, we can always re-introduce True and False.

dsnet commented 2 months ago

One advantage of this is that it makes Null at the same level of visibility as Bool, Int, etc.

In godoc, variables are given less prominence than functions (e.g., variables never show up in the table of contents, while functions do).

ChrisHines commented 2 months ago

I think it is fine. We did the same thing for "constant" values in go-kit/log/level.

https://github.com/go-kit/log/blob/0b69c7049332e99c25d5fd0f4d08317cfe45e7d8/level/level.go#L204-L239

mvdan commented 2 months ago

SGTM with the assumption the compiler is/will be clever enough to handle this efficiently

Zxilly commented 2 days ago

I prefer the old scheme. If it's for immutability, can we set these enums to const string and expose the rawToken helper function?