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

tidy code generation, add KnownCodes, add Code.Tag method #59

Merged
merged 4 commits into from Dec 2, 2021
Merged

tidy code generation, add KnownCodes, add Code.Tag method #59

merged 4 commits into from Dec 2, 2021

Conversation

mvdan
Copy link
Contributor

@mvdan mvdan commented Nov 23, 2021

As described in #58 (comment). See the individual commit messages - please do not squash.

The only open question to my mind is how the list of codes has a duplicate - Ipfs and Libp2p are both 0x01a5. Right now, both have constants and entries in KnownCodes, but Ipfs (the deprecated one) is omitted from the Tag method as duplicate switch cases aren't allowed. I guess this makes it consistent that we support both, as using the Tag method on either will work, given they have the same tag attribute.

The alternative would be to consistently omit Ipfs everywhere - its constant and its entry in KnownCodes. I lean towards not doing that given how "deprecated" isn't well defined upstream.

cc @schomatis @aschmahmann

A few tags got changed; no noteworthy code changes.
Now that Go 1.17 came out four months ago,
we can clean up the go:generate comments.

We don't need to worry about supporting Go 1.16,
as "go generate" only needs to work for the few maintainers.

Fixes #41.
Right now, this is simply backed by a code-generated slice,
but the API being a function gives us some wiggle room in the future.

The test simply ensures the list is reasonably sane;
that it has many codes, no unexpected duplicates,
and that a few known ones are present in it.

Updates #58.
@mvdan
Copy link
Contributor Author

mvdan commented Nov 23, 2021

Both KnownCodes and the Tag method could use smarter code in the future if the number of multicodecs gets large to a point where we start worrying about binary sizes or memory usage. For the time being, I think a global slice and a big switch are fine.

Copy link
Member

@rvagg rvagg left a comment

Choose a reason for hiding this comment

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

Neat to see them organised into tag groups

t.Fatalf("expected list to have at least 100 elements")
}
seen := map[multicodec.Code]bool{}
missing := map[multicodec.Code]bool{
Copy link
Contributor

Choose a reason for hiding this comment

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

this isn't 'missing' so much as '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.

I actually thought about this for half a minute :) it's "expected" when declared, and "missing" when used at the end of the function. so I found "missing" more intuitive: we declare it, update it via the loop, and are left with... the missing bits.

@schomatis
Copy link

@mvdan Thanks, let's keep this open please until I can test it in the go-ipfs commands.

@mvdan
Copy link
Contributor Author

mvdan commented Nov 24, 2021

@mvdan Thanks, let's keep this open please until I can test it in the go-ipfs commands.

Sounds good - no rush from my part.

{{- end }}
}

func (c Code) Tag() string {

Choose a reason for hiding this comment

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

Can these tags be declared as constants (similar to the codes) to quickly discover the available/existing ones? (instead of needing to browse the long switch)

Choose a reason for hiding this comment

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

(just a nit, not blocking on my end)

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; in the original issue I pondered whether this should be an enum rather than a string, but I also don't think upstream treats tags as fixed, stable constants. They're just metadata strings, as far as I can tell. I worry that using enums would give the false sense that they're indeed fixed forever, just like the multicodec integers are.

Maybe @rvagg @vmx or @willscott have thoughts.

If we don't want to declare an enum, we could still make the enumeration of known tags public, via something like func KnownTags() string :) I initially thought that would be unnecessary, but I'm happy to add it if go-ipfs needs it. What use case do you have? I thought you'd essentially just query tags and compare them against known ones like ipld.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, metadata rather than "fixed, stable constants", there's some amount of ambivalence toward the tags because categorisation is a tricky thing in many cases so the edges are squishy (as demonstrated by this serialization->ipld switch). I'd prefer that they not be raised too much in prominence in our APIs.

We're going to switch from a CSV to a more flexible format for input at some point (CSV will still exist, but a JSON can hold much more information), so that will give us additional ability to describe and categorise entries. Perhaps even having multiple tags for individual entries. So for now, don't push "tag" too far.

Choose a reason for hiding this comment

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

Again, this is just a nit, feel free to ignore. The suggested pattern is not having hard-coded strings scattered in a giant switch, but this is generated code from a spec'ed table so it's fine.

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 going to keep it as a string for now, then. If the upstream CSV becomes richer, or starts declaring tags as fixed constants, then I'll be more than happy to update the API to reflect that.

Copy link

@schomatis schomatis left a comment

Choose a reason for hiding this comment

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

Works for go-ipfs.

@mvdan
Copy link
Contributor Author

mvdan commented Dec 2, 2021

Three reviews and it works for go-ipfs - merging :) Thanks all.

@mvdan mvdan merged commit 1418219 into multiformats:master Dec 2, 2021
@mvdan
Copy link
Contributor Author

mvdan commented Dec 3, 2021

@schomatis @aschmahmann let me know when you're done integrating with this, and I'll be happy to tag v0.4.0.

@mvdan mvdan deleted the gen-table branch December 22, 2021 09:50
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

5 participants