-
Notifications
You must be signed in to change notification settings - Fork 741
Add version, kind, and spec to lineage schema #6075
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
22be0c2
add spec field to json model
jorgee 0ad076c
Fix tests
jorgee 9f7c0fc
Update typo in message [ci fast]
jorgee 5a5d5be
Merge branch 'master' into lineage-spec-json-model
pditommaso 24358ca
[ci fast] Merge branch 'master' into lineage-spec-json-model
pditommaso 1ca9f43
Minor change [ci fast]
pditommaso f52d7a8
Add backward compatibility
pditommaso 64b3f0b
Update modules/nf-commons/src/main/nextflow/serde/gson/RuntimeTypeAda…
pditommaso 7584275
Add new line [ci skip]
pditommaso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,15 +16,13 @@ | |
| package nextflow.lineage.serde | ||
|
|
||
| import com.google.gson.Gson | ||
| import com.google.gson.JsonElement | ||
| import com.google.gson.JsonObject | ||
| import com.google.gson.JsonParseException | ||
| import com.google.gson.JsonParser | ||
| import com.google.gson.TypeAdapter | ||
| import com.google.gson.reflect.TypeToken | ||
| import com.google.gson.stream.JsonReader | ||
| import com.google.gson.stream.JsonWriter | ||
|
|
||
| import groovy.transform.CompileStatic | ||
| import nextflow.lineage.model.v1beta1.FileOutput | ||
| import nextflow.lineage.model.v1beta1.LinModel | ||
|
|
@@ -34,15 +32,16 @@ import nextflow.lineage.model.v1beta1.Workflow | |
| import nextflow.lineage.model.v1beta1.WorkflowOutput | ||
| import nextflow.lineage.model.v1beta1.WorkflowRun | ||
| import nextflow.serde.gson.RuntimeTypeAdapterFactory | ||
|
|
||
| /** | ||
| * Class to serialize LiSerializable objects including the Lineage model version. | ||
| * | ||
| * @author Jorge Ejarque <[email protected]> | ||
| */ | ||
| @CompileStatic | ||
| class LinTypeAdapterFactory<T> extends RuntimeTypeAdapterFactory<T> { | ||
|
|
||
| public static final String VERSION_FIELD = 'version' | ||
| public static final String SPEC_FIELD = 'spec' | ||
| public static final String CURRENT_VERSION = LinModel.VERSION | ||
|
|
||
| LinTypeAdapterFactory() { | ||
|
|
@@ -53,7 +52,6 @@ class LinTypeAdapterFactory<T> extends RuntimeTypeAdapterFactory<T> { | |
| .registerSubtype(TaskRun, TaskRun.simpleName) | ||
| .registerSubtype(TaskOutput, TaskOutput.simpleName) | ||
| .registerSubtype(FileOutput, FileOutput.simpleName) | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -70,39 +68,43 @@ class LinTypeAdapterFactory<T> extends RuntimeTypeAdapterFactory<T> { | |
| return new TypeAdapter<R>() { | ||
| @Override | ||
| void write(JsonWriter out, R value) throws IOException { | ||
| def json = delegate.toJsonTree(value) | ||
| if (json instanceof JsonObject) { | ||
| json = addVersion(json) | ||
| } | ||
| gson.toJson(json, out) | ||
| final object = new JsonObject() | ||
| object.addProperty(VERSION_FIELD, CURRENT_VERSION) | ||
| String label = getLabelFromSubtype(value.class) | ||
| if (!label) | ||
| throw new JsonParseException("Not registered class ${value.class}") | ||
| object.addProperty(getTypeFieldName(), label) | ||
| def json = gson.toJsonTree(value) | ||
| object.add(SPEC_FIELD, json) | ||
| gson.toJson(object, out) | ||
| } | ||
|
|
||
| @Override | ||
| R read(JsonReader reader) throws IOException { | ||
| def json = JsonParser.parseReader(reader) | ||
| if (json instanceof JsonObject) { | ||
| def obj = (JsonObject) json | ||
| def versionEl = obj.get(VERSION_FIELD) | ||
| if (versionEl == null || versionEl.asString != CURRENT_VERSION) { | ||
| throw new JsonParseException("Invalid or missing version") | ||
| } | ||
| final obj = JsonParser.parseReader(reader)?.getAsJsonObject() | ||
| if( obj==null ) | ||
| throw new JsonParseException("Parsed JSON object is null") | ||
| final versionEl = obj.get(VERSION_FIELD) | ||
| if (versionEl == null || versionEl.asString != CURRENT_VERSION) { | ||
| throw new JsonParseException("Invalid or missing '${VERSION_FIELD}' JSON property") | ||
| } | ||
| final typeEl = obj.get(getTypeFieldName()) | ||
| if( typeEl==null ) | ||
| throw new JsonParseException("JSON property '${getTypeFieldName()}' not found") | ||
|
|
||
| // Check if this is the new format (has 'spec' field) or old format (data at root level) | ||
| final specEl = obj.get(SPEC_FIELD)?.asJsonObject | ||
| if ( specEl != null ) { | ||
| // New format: data is wrapped in 'spec' field | ||
| specEl.add(getTypeFieldName(), typeEl) | ||
| return (R) delegate.fromJsonTree(specEl) | ||
| } else { | ||
| // Old format: data is at root level, just remove version field | ||
| obj.remove(VERSION_FIELD) | ||
| return (R) delegate.fromJsonTree(obj) | ||
| } | ||
| return delegate.fromJsonTree(json) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static JsonObject addVersion(JsonObject json){ | ||
| if( json.has(VERSION_FIELD) ) | ||
| throw new JsonParseException("object already defines a field named ${VERSION_FIELD}") | ||
|
|
||
| JsonObject clone = new JsonObject(); | ||
| clone.addProperty(VERSION_FIELD, CURRENT_VERSION) | ||
| for (Map.Entry<String, JsonElement> e : json.entrySet()) { | ||
| clone.add(e.getKey(), e.getValue()); | ||
| } | ||
| return clone | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.