typelevel / cats-uri Goto Github PK
View Code? Open in Web Editor NEWURI implementation based on cats-parse with cats instances
License: MIT License
URI implementation based on cats-parse with cats instances
License: MIT License
I thought it worth suggesting one way of building a URI library that has the following advantages:
I have used this concept for a very much larger library banana-rdf scala3 branch. The key interface is RDF which gives the main types for RDF, and then Ops which gives the key operations on each type, which are specified in the operations package.
These then get implemented for the various frameworks, such as Apache Jena or IBM Eclipse Rdf4J or even a JavaScript project such as Tim Berners-Lee's rdflib.js...
One can then write code independent of the implementation and switch between implementations by just changing the ops. (One can hard code that for a particular project to avoid the abstraction). For the abstract view see the test cases such as GraphTest.scala.
As I was writing some code to sign http headers, I actually started on this project for HTTP frameworks, abstracting between Akka and Http4s Messages. See httpSig's Http.scala with an Http4s implementation and an Akka implementation. I did not spend that much time on that as I was just trying to reduce duplication of code in tests and elsewhere.
The URI, URL and URN seem to be very similar to those use cases. Just as for RDF and HTTP all implementations build on the same standards, so that it is not surprising that there is a common abstraction.
One could thus start by implementing it for
|
)and once tests are written to cover those, one can then write a pure scala implementation that can be confident it has captures the URI abstractions faithfully.
Having tried lemonlabs Uri in banana-rdf and using http4s URi I have some ideas of key elements to improve http4s Uri.
Http4s Uris are currently actually URLs, and should be named as such. Urls can have paths, be relativised, absolutized and be used for requests using something like an http framework. URNs cannot. We have type URI = URL | URN
Http4s should take onboard the typing lemonlabs uses, that clearly distinguishes absolute Urls, relative Urls (and there relative path absolute: urls starting with /
) and also schema relative urls. That would allow Http4s to let us know if a Request is meant to take an absolute or a relative Url for example. There may be clients that can take both, but not all clients can. A client that has no connection cannot take a relative Url. A client bound to a connection can take a (path absolute) relative Url).
Further crazy requirements:
Ideally (here we may be going beyond what is possible) it would be possible for the interface to be implemented so as to allow unique storage of URIs to re-use components. So one would be able to have a garbage collected store of URIs starting with "https" then followed by domain components, etc... so that Uris could be compare using eq
for equality and use as little memory as possible.
In RDF one has so many URIs the frameworks are careful to minimally parse them, leaving perhaps invalid uris in graphs rather than having to parse the structure of them. I wonder if there is a clever way of optionally integrating that requirement.
Perhaps one also wants to map URIs to 64bit Long numbers. At the RDF layer one does not need to look into the URIs at all: one could just as well use 64bit long comparison to test for equality, once those are parsed and stored in a DB.
Having an excellent real URI class that encompasses both URNs and URLs would be very helpful when writing application code. On the web application code proceeds as follows:
If the code could use the same URI structure in the parsing stage as it uses in the fetching stage that would make for much nicer and smoother coding. Note in RDF URIs are the core of the naming scheme, so one is using them all the time for every data structure. In banana-rdf I added support for relative urls, as those are useful when fetching data and also when posting graphs to a container: the server decides on the URL of the created resource and so the graph sent has to be one that can contain relative Urls.
I also write this up on the http4s discussion area http4s/http4s#5930 (comment)
It would help to have a URI spec that can abstract between any implementation of URIs, the way that banana-rdf can abstract between any RDF Framework, without needing to create any new objects (using opaque types essentially and Match Types.
Having such a library would allow one to write code that can work in Java, JS, and pure Scala land and native using the libs most appropriate for that platform, and making a switch from one library to the next a simple 1 line code change.
That would allow one to write code that could use any of the following libs:
The pattern for such an abstraction is demonstrated with the banana-rdl scala3 library:
To see how it is used, please take a look at the tests which are framework independent, such as this one:
https://github.com/bblfish/banana-rdf/blob/scala3/rdf-test-suite/shared/src/main/scala/org/w3/banana/GraphTest.scala
Banana-rdf actually has an initial abstraction for URI too:
rg/w3/banana/operations/URI.scala, to help us write code that can work with URIs across frameworks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.