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

Issue #45: Add missing spec methods to DataFactory interface #46

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

matthieubosquet
Copy link
Contributor

As per issue #45 the fromTerm and fromQuad DataFactory methods defined by the RDF/JS DataModel specification are not reflected in the @rdfjs/types package.

This PR adds those methods.

That said, I am unsure in which context those methods would be used and didn't find an explanation of when it would be or whether the omission is intentional.

Copy link

changeset-bot bot commented Sep 9, 2024

🦋 Changeset detected

Latest commit: bd385b4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@rdfjs/types Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

data-model.d.ts Outdated Show resolved Hide resolved
@jeswr
Copy link
Contributor

jeswr commented Sep 10, 2024

Hi @matthieubosquet :)

Thanks for these changes! It may be worth using a mver bump to release these changes, as many RDF/JS implementations do not implement these methods (c.f. rdfjs/N3.js#447) - and so this will either cause (1) the package to declare methods that it does not implement or (2) have type errors if the package is, e.g., written in typescript and implements the DataFactory interface.

@matthieubosquet
Copy link
Contributor Author

Rebased and made this a major bump: b0ca1a9.

I am not sure what is the release process and how to publish a v1.1.1 (maybe before merging this).

I also wonder if maybe we could add a few items before potentially publishing a version 2.0.0:

  1. simplify the release process, document it and update the publish action accordingly (it feels like changeset and yarn might be a tad overkill and extra tooling to learn)
  2. update dependencies (typescript at least, hopefully eslint too)

Copy link
Contributor

@tpluscode tpluscode left a comment

Choose a reason for hiding this comment

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

Please see inline comment

CHANGELOG.md Outdated Show resolved Hide resolved
@tpluscode
Copy link
Contributor

If we want to simultaneously work on v2 then we'd probably need a dedicated branch...

Copy link
Contributor

@tpluscode tpluscode left a comment

Choose a reason for hiding this comment

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

Thanks for the changeset @matthieubosquet 👌

I took a closer look and I agree with the other to make this a major release. A little unfortunate, because this may disrupt the ecosystem when some packages start to depend on @rdfjs/types@^2 while other stay on v1. But maybe I'm wrong we'll see

I also added some more suggestions (was on mobile earlier)

.changeset/afraid-donuts-retire.md Outdated Show resolved Hide resolved
data-model.d.ts Outdated Show resolved Hide resolved
rdf-js-tests.ts Outdated Show resolved Hide resolved
rdf-js-tests.ts Outdated Show resolved Hide resolved
rdf-js-tests.ts Outdated Show resolved Hide resolved
data-model.d.ts Outdated Show resolved Hide resolved
@matthieubosquet
Copy link
Contributor Author

Thanks for the suggestions @tpluscode and @rubensworks!

I fixed up the tests after applying suggestions: f1879fb.

The DataFactory initialised with const dataFactory: DataFactory<BaseQuad> = <any> {}; was returning a BaseQuad, not a Quad, hence CI failure.

Copy link
Contributor

@tpluscode tpluscode left a comment

Choose a reason for hiding this comment

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

Great work. Thanks. I think we're good now

Copy link
Member

@rubensworks rubensworks left a comment

Choose a reason for hiding this comment

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

Tested in rdf-data-factory, and everything seems to work as intended!

@rubensworks rubensworks mentioned this pull request Sep 17, 2024
data-model.d.ts Outdated
fromTerm<T extends Literal>(original: T): Literal;
fromTerm<T extends Variable>(original: T): Variable;
fromTerm<T extends DefaultGraph>(original: T): DefaultGraph;
fromTerm<T extends Quad>(original: T): OutQuad;
Copy link
Contributor

Choose a reason for hiding this comment

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

BaseQuad input should also presumably be supported since BaseQuads are valid Terms

fromTerm<T extends Literal>(original: T): Literal;
fromTerm<T extends Variable>(original: T): Variable;
fromTerm<T extends DefaultGraph>(original: T): DefaultGraph;
fromTerm<T extends Quad>(original: T): OutQuad;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you revert this?

@matthieubosquet
Copy link
Contributor Author

After @jeswr's comment, I realised that there might be a bit of inconsistency in the DataFactory method declarations and its use of generic types.

In a nutshell, with a typing such as: fromTerm<T extends NamedNode>(original: T): NamedNode;, the DataFactory would take in an extended NamedNode and produce a NamedNode.
I don't think that's what we want for DataFactory implementers.
With a typing such as: fromTerm<T extends Term>(original: T): T;, if we pass an extended NamedNode to the DataFactory, it will return the extended type which still can be cast to a NamedNode (see corresponding test).
Isn't it closer to what DataFactory implementers might want?


I tried to produce a commit that illustrates and offers a compromise to fix the issue.


Remains an inconsistency with the quad factory part:
We are currently offering to define InQuad and OutQuad as anything that extends BaseQuad.
That allows us to be specific as to what the DataFactory consumes and produces (as far as quads are concerned).

If we allow that amount of specificity for Quads, is there a specific reason not to allow it for other Terms?

@jeswr
Copy link
Contributor

jeswr commented Sep 18, 2024

I don't think that's what we want for DataFactory implementers.

I think I see where you're going, but this solution doesn't quite work. Consider using fromTerm from @rdfjs/data-model with an input of an N3.js NamedNode. The N3.js NamedNode has an extra #toJson method that will not be available in the NamedNode output by the @rdfjs/data-model method; however the type inference you have now would suggest that the output NamedNode does have this #toJson method.

What you want to allow for is for the libraries to extend the DataFactory types such that any custom extensions they have to their terms can be included - that is fromTerm from N3.js will always output a NamedNode with the #toJson method regardless of whether the input NamedNode does or not. Your original implementation should permit this.

@tpluscode
Copy link
Contributor

To demonstrate the problem, here's see this playground link with the generic function.

Here's what @jeswr described. The var converted is typed as NamedNodeExt which is probably incorrect.

@tpluscode
Copy link
Contributor

I think it's worth mentioning the PR #24 which could allow implementors to define concrete term subtypes returned by a data factory

@blake-regalia would you interested in picking up where we left that?

@jeswr
Copy link
Contributor

jeswr commented Sep 19, 2024

@tpluscode and any interested others in this thread; could you please review DefinitelyTyped/DefinitelyTyped#70614

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.

4 participants