Skip to content

feat: Update ChainedConverter to create dynamic paths - #697

Merged
jvherweg merged 2 commits into
mainfrom
feat/automatic-conversion
Apr 27, 2021
Merged

jvherweg merged 2 commits into
mainfrom
feat/automatic-conversion

Conversation

@jvherweg

Copy link
Copy Markdown
Member

Closes #484, see discussion there as well.

Instead of creating a new class I updated ChainedConverter since we would have no need for that one anymore after this.

The canHandle call of this one is going to be more expensive than most other classes, but this should always be the last handler that gets checked so this should be fine.

There are some parts that could be move to separate files: the cache could be a separate class file, some functions could be moved to ConversionUtil, but they are very specific to this implementation so it made sense to me to keep them there.

@rubensworks rubensworks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You could probably go very far with optimizing this one, but it's probably better to wait with that until actual performance issues are encountered. With the relatively small number of media types we have atm, this impl should be fast enough.

@jvherweg

Copy link
Copy Markdown
Member Author

You could probably go very far with optimizing this one, but it's probably better to wait with that until actual performance issues are encountered.

I agree, that's why I added a bunch of suggestions should we ever reach that point (which I think we'll never will tbh).

@RubenVerborgh

Copy link
Copy Markdown
Member

I'd definitely want to test with https://github.com/RubenVerborgh/solid-hue/blob/main/config/config-hue.json#L68-L91, when the current limitations became apparent to me.

@jvherweg

Copy link
Copy Markdown
Member Author

It's not urgent to merge so I'll hold of until you could have a look at it. In that config the entire solid-hue:RepresentationConverter block can now be replaced with

    {
      "@id": "solid-hue:RepresentationConverter",
      "@type": "ChainedConverter",
      "ChainedConverter:_converters": [
        { "@type": "HueToQuadConverter" },
        { "@type": "QuadToHueConverter" }
      ]
    }

@RubenVerborgh RubenVerborgh self-assigned this Apr 20, 2021
@RubenVerborgh

Copy link
Copy Markdown
Member

In that config the entire solid-hue:RepresentationConverter block can now be replaced with

If I do that, there seems to be a problem with */*:

NotImplementedHttpError: Cannot convert from internal/vnd.philips.hue+json to text/html,application/xhtml+xml,image/webp,application/xml,*/*, only from internal/vnd.philips.hue+json to internal/vnd.philips.hue+json.
    at Object.supportsMediaTypeConversion (/Users/ruben/Documents/UGent/Solid/solid-hue/node_modules/@solid/community-server/dist/storage/conversion/ConversionUtil.js:111:15)
    at ChainedConverter.canHandle (/Users/ruben/Documents/UGent/Solid/solid-hue/node_modules/@solid/community-server/dist/storage/conversion/TypedRepresentationConverter.js:44:26)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async ChainedConverter.handleSafe (/Users/ruben/Documents/UGent/Solid/solid-hue/node_modules/@solid/community-server/dist/util/handlers/AsyncHandler.js:29:9)
    at async RepresentationConvertingStore.getRepresentation (/Users/ruben/Documents/UGent/Solid/solid-hue/node_modules/@solid/community-server/dist/storage/RepresentationConvertingStore.js:23:32)
    at async /Users/ruben/Documents/UGent/Solid/solid-hue/node_modules/@solid/community-server/dist/storage/LockingResourceStore.js:75:34
    at async runWithTimeout (/Users/ruben/Documents/UGent/Solid/solid-hue/node_modules/@solid/community-server/dist/util/locking/WrappedExpiringReadWriteLocker.js:51:24)
    at async GreedyReadWriteLocker.withReadLock (/Users/ruben/Documents/UGent/Solid/solid-hue/node_modules/@solid/community-server/dist/util/locking/GreedyReadWriteLocker.js:35:20)

But equally with just text/turtle:

NotImplementedHttpError: Cannot convert from internal/vnd.philips.hue+json to text/turtle, only from internal/vnd.philips.hue+json to internal/vnd.philips.hue+json.

I also tried this, but to no avail:

    {
      "@id": "solid-hue:RepresentationConverter",
      "@type": "ChainedConverter",
      "ChainedConverter:_converters": [
        { "@type": "HueToQuadConverter" },
        { "@type": "QuadToHueConverter" },
        { "@type": "QuadToRdfConverter" },
        { "@type": "RdfToQuadConverter" }
      ]
    }

Code at https://github.com/RubenVerborgh/solid-hue/tree/feature/chainedconverter

To test if you don't have a Hue Bridge, just

diff --git i/src/PhilipsHueStore.ts w/src/PhilipsHueStore.ts
index 6ccc0e2..ba7fd8a 100644
--- i/src/PhilipsHueStore.ts
+++ w/src/PhilipsHueStore.ts
@@ -48,10 +48,7 @@ export class PhilipsHueStore extends BaseResourceStore {
     const url = templates.lights.expand(this.settings);
     this.logger.debug(`Retrieving light status from ${url}`);

-    const response = await this.fetcher.fetch(url);
-    assert.equal(response.status, 200);
-
-    return new BasicRepresentation(await response.text(), identifier, HueContentType);
+    return new BasicRepresentation('{}', identifier, HueContentType);
   }

   /**

@RubenVerborgh RubenVerborgh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does not work yet with solid-hue.

@jvherweg

Copy link
Copy Markdown
Member Author

Don't have time yet to look into this, but I do wonder how that error gets thrown since the only time that specific string gets thrown is when doing a canHandle check on a TypedRepresentationConverter. But the new ChainedConverter is no longer a TypedRepresentationConverter and never calls canHandle or handleSafe on any of its input converters.

But on the other hand, if the old ChainedConverter code was run by accident, the error should also look different since that one takes the input types of the first converter and output types of the last one. Which in that case should give the error it can only convert from hue to quads, which is not what it says.

Probably overlooking something.

@RubenVerborgh

Copy link
Copy Markdown
Member

I do wonder how that error gets thrown

I got something wrong, apparently. Sorry for the confusion; better report below.

Configuration A

    {
      "@id": "solid-hue:RepresentationConverter",
      "@type": "ChainedConverter",
      "ChainedConverter:_converters": [
        { "@type": "HueToQuadConverter" },
        { "@type": "QuadToHueConverter" },
      ]
    }

(Note that the trailing comma makes this invalid JSON, but it still works. @rubensworks?)

GET: works
PUT: fails with NotImplementedHttpError: No conversion path could be made from text/turtle to internal/vnd.philips.hue+json.

Configuration B

    {
      "@id": "solid-hue:RepresentationConverter",
      "@type": "ChainedConverter",
      "ChainedConverter:_converters": [
        { "@type": "HueToQuadConverter" },
        { "@type": "QuadToHueConverter" },
        { "@type": "RdfToQuadConverter" }
      ]
    }

GET: works
PUT: works

@rubensworks

Copy link
Copy Markdown
Contributor

(Note that the trailing comma makes this invalid JSON, but it still works. @rubensworks?)

Yeah, I also noticed that before. Apparently the jsonparse lib accepts this...
Because of this and some other reasons I'm considering switching to a different streaming JSON parser, or making one myself if needed.

@jvherweg

Copy link
Copy Markdown
Member Author

What is being PUT in configuration A @RubenVerborgh ? Because the error is correct, it can't convert from turtle to hue+json with that configuration since it only has the hue converters there, not the rdf ones.

@RubenVerborgh

Copy link
Copy Markdown
Member

@joachimvh The PUT body is text/turtle.

I think the solution is acceptable indeed, however:

  • Why the asymmetry? The conversion from hue+json (via quads) to Turtle on GET works fine.
  • Can we do anything to fix that in the future?

@jvherweg

Copy link
Copy Markdown
Member Author
  • Why the asymmetry? The conversion from hue+json (via quads) to Turtle on GET works fine.

That's the interesting part then actually, because that should not work. I'm wondering what converter is doing that then. Might have something to do with the other convertingstore it passes through then (from the default config), but I can't immediately say why it works.

@RubenVerborgh

Copy link
Copy Markdown
Member

Might have something to do with the other convertingstore it passes through then (from the default config)

That's my guess. But still weird it doesn't work the other way.

The logs with config A for GET:

2021-04-21T09:10:07.196Z [ChainedConverter] debug: Converting internal/vnd.philips.hue+json ->  -> */*.
2021-04-21T09:10:07.196Z [IfNeededConverter] debug: Conversion needed for http://localhost:3000/home/lights from internal/quads to satisfy text/html;q=1, application/xhtml+xml;q=1, image/webp;q=1, application/xml;q=0.9, */*;q=0.8
2021-04-21T09:10:07.197Z [ChainedConverter] debug: Converting internal/quads ->  -> */*

The logs with config A for PUT:

none (!)

The logs with config B for PUT:

2021-04-21T09:13:15.719Z [ChainedConverter] debug: Converting text/turtle -> internal/quads -> internal/vnd.philips.hue+json.

I wonder if the asymmetry has to do with the inType:

    {
      "@id": "solid-hue:RdfEnabledPhilipsHueStore",
      "@type": "RepresentationConvertingStore",
      "RepresentationConvertingStore:_source": {
        "@id": "solid-hue:PhilipsHueStore"
      },
      "RepresentationConvertingStore:_options_outConverter": {
        "@id": "solid-hue:RepresentationConverter"
      },
      "RepresentationConvertingStore:_options_inConverter": {
        "@id": "solid-hue:RepresentationConverter"
      },
      "RepresentationConvertingStore:_options_inType": "internal/vnd.philips.hue+json"
    },

@jvherweg

Copy link
Copy Markdown
Member Author

The inType only says that that converter needs to convert to the hue format. It could do that if it got quads, but it gets turtle instead which it doesn't know how to handle. That all is as expected.

Just realized why it works when doing a GET. It's because you don't have an IfNeededConverter in your config. Which means the ChainedConverter is forced to do a conversion. The shortest path it can find is a single converter, with the only option being hue -> quads. (It doesn't support finding paths of length 0, could perhaps be added). And the second converter can handle the quads and convert those to turtle.

@RubenVerborgh

Copy link
Copy Markdown
Member

(It doesn't support finding paths of length 0, could perhaps be added)

Interesting. So probably the safest config is

    {
      "@id": "solid-hue:RepresentationConverter",
      "@type": "ChainedConverter",
      "ChainedConverter:_converters": [
        { "@type": "HueToQuadConverter" },
        { "@type": "QuadToHueConverter" },
        { "@type": "RdfToQuadConverter" },
        { "@type": "QuadToRdfConverter" }
      ]
    }

although that is a tad repetitive if there will be more things that generate quads. But good enough.

I'll just finish up my review and then this is good to go, thanks.

Comment thread test/integration/GuardedStream.test.ts Resolved
it('does not throw on async crash.', async(): Promise<void> => {
jest.useFakeTimers();
const result = await converter.handleSafe({ identifier, representation: rep, preferences: {}});
const result = await converter.handleSafe({ identifier, representation: rep, preferences: { type: { 'x/x': 1 }}});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm a bit worried that the integration test had to change. Seems like the original should still work.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This might actually be related to the issue I described of matching internal/quads to */* because I think it's the same function that handles that.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Going through this now, it actually still has to change since we were testing a chain of 2 converters here. With the new ChainedConverter it would be too smart and do the conversion with a path of length 1.

@jvherweg

Copy link
Copy Markdown
Member Author

It's also a bit special since there are 2 converters in the store chain here.

I would actually say this uncovered a bug in the new ChainedConverter: the reason it did the conversion is because the accept header had */* so the converter matched that with internal/quads. But we have failsafes to make sure this never happens, because we don't want to send internal/quads to the user. Which means I should have used the function that has those checks (or moved them around). So will need to look into this before merging.

@jvherweg
jvherweg force-pushed the feat/automatic-conversion branch from afbc981 to d659dcc Compare April 26, 2021 12:37
@jvherweg
jvherweg changed the base branch from main to refactor/conversion-util April 26, 2021 12:43
Base automatically changed from refactor/conversion-util to main April 27, 2021 14:11
@jvherweg
jvherweg merged commit 44d82ea into main Apr 27, 2021
@jvherweg
jvherweg deleted the feat/automatic-conversion branch April 27, 2021 14:12
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.

Create AutomaticConvertor to automatically chain the correct convertors

3 participants