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

Better abstractions and types #66

Closed
wants to merge 5 commits into from

Conversation

mpetrunic
Copy link
Member

Still wip but provides basic frame to start sharing more code and have better typesafety when dealing with ssz.

TODO:

  • figure out error when running tests

@mpetrunic mpetrunic marked this pull request as ready for review December 17, 2020 10:14
Copy link
Member

@wemeetagain wemeetagain left a comment

Choose a reason for hiding this comment

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

Looks really good :)
Good cleanup types with CompositeValue and ObjectLike type and Type abstract class.

Most comments are minor type changes. (all instances of object should be changed to CompositeValue) (Record<string, unknown> should be replaced with ObjectLike where possible)

Only issue I have is re: usage of isComposite.
I think its a useful method for consumers, but internal to the library, IMO its not necessary and should be replaced with type coersion as necessary.
SSZ has several invariants that don't need to be checked at runtime (basic types are always fixed-length, types are always either basic or composite)

@@ -88,7 +88,7 @@ export class BasicArrayStructuralHandler<T extends ArrayLike<unknown>> extends S
}
}

export class CompositeArrayStructuralHandler<T extends ArrayLike<object>> extends StructuralHandler<T> {
export class CompositeArrayStructuralHandler<T extends ArrayLike<ObjectLike>> extends StructuralHandler<T> {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is right?
Seems like some of the changes in this PR have object being changed to ObjectLike.
Should be CompositeValue.

Copy link
Member Author

Choose a reason for hiding this comment

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

If I put CompositeValue, that allows array element to be another array, which is perfectly fine. But our types doesn't reflect that anywhere so there is huge number of type errors. It's kinda hard because wherever I change type I cause chain reaction.

Not sure how to proceed. Maybe we can in separate PRs fix types from bottom up?

fieldType.structural.assertValidValue((value as T)[fieldName]);
fieldType.assertValidValue(value[fieldName]);
}
if (fieldType.isComposite()) {
Copy link
Member

Choose a reason for hiding this comment

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

else if?
Is this changed to fix the type? Should the ts-ignore be removed then? If the ts-ignore can't be removed, the code shouldn't be changed.

Copy link
Member Author

Choose a reason for hiding this comment

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

If you wanna add another like in this code block, it will be typesafe. I have no idea how to fix wierd error with that assertValidValue. Tried every combination. I guess you had same one since it was ignored before 😄

newValue[fieldName as keyof T] = fieldType.structural.clone(value[fieldName]);
newValue[fieldName as keyof T] = fieldType.clone(value[fieldName]) as T[keyof T];
}
if (fieldType.isComposite()) {
Copy link
Member

Choose a reason for hiding this comment

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

why is this needed now?

Copy link
Member Author

@mpetrunic mpetrunic Dec 18, 2020

Choose a reason for hiding this comment

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

Because typescript is stupid. It cannot (or at least I didn't find a way) to infer if isBasic is false that type is composite. Basically in else statement fieldType is of Type which causes type errors,

That said when going trough code. It was a lot easier to understand code with usage of isComposite to I decided to leave it on. We can make it protected so end users don't see it.

src/backings/structural/container.ts Outdated Show resolved Hide resolved
@@ -0,0 +1 @@
export type ArrayElement<T> = T extends ArrayLike<infer U> ? U : T;
Copy link
Member

Choose a reason for hiding this comment

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

Is this used anywhere?

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 think it is. I was trying to bring even more type safety to infer element type from List and Vector. Will check

src/backings/structural/vector.ts Outdated Show resolved Hide resolved
src/backings/structural/list.ts Outdated Show resolved Hide resolved

export class ContainerStructuralHandler<T extends ObjectLike> extends StructuralHandler<T> {
export class ContainerStructuralHandler<T extends Record<string, unknown>> extends StructuralHandler<T> {
Copy link
Member

Choose a reason for hiding this comment

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

ObjectLike

Copy link
Member Author

Choose a reason for hiding this comment

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

that screws everything since it ObjectLike allows empty object without keys which casues keyof to stop working

src/backings/byteArray/list.ts Outdated Show resolved Hide resolved
src/backings/byteArray/vector.ts Outdated Show resolved Hide resolved
@dapplion
Copy link
Contributor

Good ideas in this PR! But I would close for now since the codebase has changed substantially since #82. If this effort is gonna be pursued in the future better start from a new branch from current master

@dapplion dapplion closed this Aug 17, 2021
wemeetagain pushed a commit that referenced this pull request Aug 26, 2021
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.

3 participants