Closed iSnow closed 3 years ago
@iSnow It's really hard to say for me because I don't use Java but it seems a reasonable change. BTW as a client how can I use the lib comparing these 3 options?
Hi, considering you aren't that familiar with Java, I won't directly write out code examples, but pseudo code. I am trying to be fair to the current API, eg. Case 4 is shorter in the current setup, but binds you to one certain JSON lib (Java, unfortunately, has many).
I'd like to point you to Case 5 & 6. Similar setup, one time the datapackage.json
is directly referenced via URL (in this case, the library would need to store the base URL to load relative resources, but currently, it doesn't if I am not very wrong). In the other case, a ZIP file is referenced via URL. What you can see is that the current API needs to be used very differently between those cases.
It would still be good to get @georgeslabreche to give his opinion on the proposed change.
So, some scenarios how to use it:
fromSource()
build()
. The library would get the parent from the File object and know where to look for the resourcesfromSource()
build()
InputStream
from blob (everything stays in RAM)InputStream
InputStream
from blob (everything stays in RAM)fromSource()
build()
fromSource()
build()
But what happens if the application doesn't use the json.org JSON library, but the GSON from Google? Then:
fromSource()
build()
InputStream
from ZIP's URL InputStream
InputStream
from ZIP's URL fromSource()
build()
fromSource()
build()
(should do resolution of relative URLs in Resources)Hi guys!
I really like these proposals and agree that they are an improvement with respect to streamlining.
It's been a while so it's difficult for me to remember the exact details as to why it was implemented the way it is now. However, I do recall that special attention was made to stay true to the specifications as well as preserving consistent behaviour across implementations in other languages.
If we still want to enforce cross-language consistency then these proposals need to be compared with how the library has been implemented in other languages.
Hi @georgeslabreche,
great to hear from you! I do recognize that you were following the interface draft very closely, which is a good thing. In multi-language projects though, you will always have a certain tension between what is proposed as a common interface and what is true to the nature of each language. It is a valid decision to stick to a common interface as closely as possible, but I'd argue that it's permissible to take some liberties to create idiomatic code for each environment, not least because the implementation guide states:
We prefer to focus on actions rather than features, feature sets, user stories, or more formal API specifications as we want to leave enough flexibility for implementations that follow the idioms of the host language, yet we do want to ensure a common base of what can be done with an implementation in any language.
There is no absolute right or wrong here, but my feeling when it comes to different language implementations, is that it's better to focus on what to achieve (the actions) than the how (precise API). Even more so because few developers constantly switch between Python, Java and JS.
I totally agree. We just need an A-OK or No-Go from the frictionlessdata peeps. @roll?
Changes implemented close to proposal #1, so no builder pattern.
Overview
Besides the no-args constructor, the
Package
class has 8 constructors, half of them just convenience functions (See "AS IS" below).The way the API is structured is also not very Java-like, as
URL-based constructors are limited to HTTP/HTTPS protocols according to the JavaDoc, but should (and probably do) also work for file-URLs.
Usage of filepaths in the form of Strings is at least unusual and feels like a direct Python-port. A Java-API preferably should use either File/Path or InputStream (which would cover URLs, Files, even JSON-Strings if need be)
Using a String to either hold JSON-content or a file-path to a ZIP-file in
public Package(String jsonStringSource, boolean strict)
is rather unusual for a Java-API.Proposal 1: Trim and make idiomatic Java
It would be sufficient to trim the host of constructors to three, maybe four:
The most fundamental constructor would rely on an
InputStream
, the other constructors are syntactic sugar and would simply create an InputStream on either the URL or the JSON-String and delegate to this.It would be necessary to validate that this setup supports ZIP-packaged DataPackages, but that should be possible. Going through the code, it seems that
Resource
resolution has a handful of problems in both directory-based datapackages with resources (paths can't really be relative to thedatapackage.json
) and ZIP-packaged DataPackages, but I would have to look into this more deeply.Proposal 2: Switch to a Builder-pattern
Since fluid interfaces are very much a part of Java since a couple of years, it would maybe make sense to go with the practice and switch over to a builder-based API:
The special method for reading from a ZIP file might be needed to read
Resource
s from inside the ZIP-files, I am not totally sure.If there's any interest in this, I would volunteer to work on either proposal to demo its validity.
AS IS:
Please preserve this line to notify @georgeslabreche (maintainer of this repository)