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

Add a Compare function #309

Open
chrisfenner opened this issue Sep 8, 2022 · 0 comments
Open

Add a Compare function #309

chrisfenner opened this issue Sep 8, 2022 · 0 comments
Labels
tpmdirect Issues specific to the tpmdirect development work

Comments

@chrisfenner
Copy link
Member

Since a lot of the tpmdirect structs have private members, we should create a convenient way to compare two values without hassling with adding enough types into cmpopts.IgnoreUnexportedFields.

@chrisfenner chrisfenner added the tpmdirect Issues specific to the tpmdirect development work label Sep 8, 2022
chrisfenner added a commit that referenced this issue Feb 9, 2023
This is unfortunately a large change, but I think it does a lot for the ergonomics of the TPMDirect API.

This change uses the new Go 1.18 generics to solve a few problems:

- We want people to be able to provide a flat `[]byte` or actual structure when instantiating TPM2Bs
- We want to avoid people directly manipulating pointer values in the TPMUs or having their TPMUs in an invalid state
- We want a nice Marshal and Unmarshal function (and later, to be able to make a nice Compare function, see #309 )

Generics to the rescue. Here's what this commit does:

- Add a new file called `marshalling.go` that handles a lot of the high level marshalling work. `reflect.go` is still the dirty reflection guts of the library
- Embed a new type called `marshalByReflection` into all the structs that can be marshalled by reflection, as a clear hint to the reflection library
- Add a new interface called `UnmarshallableWithHint` - most of the TPMU implement this, and the old `marshalUnion` and `unmarshalUnion` functions are gone now
- Bonus: I noticed using profiling that the `tags` function was allocating several orders of magnitude more memory than the rest of the library, so I rewrote it
- Introduced a generic TPM2B helper that is aliased by the concrete TPM2B types; there are constructors for instantiating TPM2B from data or structured contents.
- TPMU is public, with private fields. Introduced constructors for these with type constraints.

Fixes #307 and #292.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tpmdirect Issues specific to the tpmdirect development work
Projects
None yet
Development

No branches or pull requests

1 participant