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

proposal: generalize toReadable #199

Closed
giacomociti opened this issue Nov 5, 2023 · 3 comments · Fixed by #250
Closed

proposal: generalize toReadable #199

giacomociti opened this issue Nov 5, 2023 · 3 comments · Fixed by #250

Comments

@giacomociti
Copy link
Contributor

the base package has toReadable operations (although they are not listed in manifest.ttl).

I propose a new implementation, simpler and more general:

function toReadable() {
  return Readable.from(arguments)
}

with manifest entry:

<toReadable> a p:Operation, p:ReadableObjectMode;
  rdfs:label "To Readable";
  rdfs:comment "Emits arguments as chunks.";
  code:implementedBy [ a code:EcmaScriptModule;
    code:link <node:barnard59-base/toReadable.js#default>
  ].

The proposed implementation is not limited to a single value:

@prefix p: <https://pipeline.described.at/> .
@prefix base: <https://barnard59.zazuko.com/operations/base/> .
@prefix ntriples: <https://barnard59.zazuko.com/operations/formats/ntriples/> .
@prefix fs: <https://barnard59.zazuko.com/operations/rdf/fs.js#> .

<> a p:Pipeline ;
  p:steps [ 
    p:stepList
      (
        [ base:toReadable ("file1.ttl" "file2.ttl") ]
        [ fs:parse () ]
        [ ntriples:serialize () ]
        [ base:stdout () ]
      ) 
  ]
.

The same implementation works also with objects (even though I see less use cases):

@prefix code: <https://code.described.at/> .
@prefix p: <https://pipeline.described.at/> .
@prefix base: <https://barnard59.zazuko.com/operations/base/> .

<> a p:Pipeline ;
  p:steps [ 
    p:stepList
      (       
        [ base:toReadable ("{ age: 34}"^^code:EcmaScript "{ age: 23}"^^code:EcmaScript) ]
        [ base:json\/stringify () ]
        [ base:stdout () ]
      ) 
  ]
.
@tpluscode
Copy link
Contributor

tpluscode commented Nov 7, 2023

That looks quite useful. Overall, a big 👍

Two questions and two nits

  1. Do you know if there is any different between Readable.from and the current implementation? Surely, it was created that way for a reason?
  2. I would actually name the operation to match - base:ReadableFrom ( "foo" "bar" )
  3. And maybe should suggest the use of ...args over arguments
  4. Does "{ age: 34 }"^^code:EcmaScript actually work to create an object from the JSON string? Neat

@tpluscode
Copy link
Contributor

Ah, and one more thing. Would this still need two versions for objectMode: true/false?

@giacomociti
Copy link
Contributor Author

Readable.from was added in node v12.3.0, v10.17.0 so I guess it was already available when the current implementation was made. I don't know why it is not taking advantage of it.

Renaming to base:ReadableFrom and using ...args are good ideas, thanks.

Objects are parsed fine, and the readable stream is created in object mode. If I'm not wrong, the stream is in object mode also when using string arguments (this is also explicitly stated in the documentation). Apparently it is working fine as a single function for both objects and strings (I tried to make separate functions but I got error when setting objectMode false for strings).

I can make a PR with the suggested changes so we can add some unit tests to ensure the behavior is correct.

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 a pull request may close this issue.

2 participants