added generation of type specific 'MarshalJSON' & 'UnmarshalJSON' implementations#98
added generation of type specific 'MarshalJSON' & 'UnmarshalJSON' implementations#98sailingKieler wants to merge 14 commits into
Conversation
| } | ||
| // We expect at this point that all references have been attempted to resolve. | ||
| if !r.resolved.Load() { | ||
| return nil, errors.New("reference not resolved") |
There was a problem hiding this comment.
Proposal: generate a partial reference like in Langium:
There was a problem hiding this comment.
This is just preliminary, I don't know which context instance to rely on for calling Ref(...), yet
(since this function is called by json.Mashall(...), not by ourselves), and whether we should attempt to resolve here, at all.
AFAICS, r.resolved is true if resolution has been attempted, regardless of success or failure.
Hence, this guards stops the serialization, e.g., if the document hasn't been build until DocStateLinked at least.
| RefText string `json:"refText"` | ||
| URI string `json:"uri,omitempty"` | ||
| Err string `json:"error,omitempty"` | ||
| }{ |
There was a problem hiding this comment.
This struct def should be exported. And I propose aligning this with Langium for cross-framework compatibility:
There was a problem hiding this comment.
This struct def should be exported.
Why?
It's purely technical and kind of a vehicle.
It might make sense to rely in the two (Un)Marshal methods on the same definition, though.
And I propose aligning this with Langium for cross-framework compatibility:
That's my plain, too.
| Err string `json:"error,omitempty"` | ||
| }{ | ||
| RefText: r.unit.String(), | ||
| URI: uri, |
There was a problem hiding this comment.
In Langium, we omit the URI if the reference target is in the same document. This can reduce the size of generated JSON by a lot.
Thought: can we include something like a URI converter here as well? Exporting JSONs with absolute file URIs would be pretty useless in many cases.
There was a problem hiding this comment.
In Langium, we omit the URI if the reference target is in the same document. This can reduce the size of generated JSON by a lot.
That's achieved by the omitempty setting. Of course, the prop value shouldn't be set upfront.
Thought: can we include something like a URI converter here as well? Exporting JSONs with absolute file URIs would be pretty useless in many cases.
Yes, that's my plan.
sailingKieler
left a comment
There was a problem hiding this comment.
Thanks for the hints Miro.
This state is a very early one, it doesn't represent the one I have in mind already.
I also seek for a solution that is compatible with Langium, s.t. we could use it as an interface for data exchange
More changes are coming soon :-)
| Err string `json:"error,omitempty"` | ||
| }{ | ||
| RefText: r.unit.String(), | ||
| URI: uri, |
There was a problem hiding this comment.
In Langium, we omit the URI if the reference target is in the same document. This can reduce the size of generated JSON by a lot.
That's achieved by the omitempty setting. Of course, the prop value shouldn't be set upfront.
Thought: can we include something like a URI converter here as well? Exporting JSONs with absolute file URIs would be pretty useless in many cases.
Yes, that's my plan.
| RefText string `json:"refText"` | ||
| URI string `json:"uri,omitempty"` | ||
| Err string `json:"error,omitempty"` | ||
| }{ |
There was a problem hiding this comment.
This struct def should be exported.
Why?
It's purely technical and kind of a vehicle.
It might make sense to rely in the two (Un)Marshal methods on the same definition, though.
And I propose aligning this with Langium for cross-framework compatibility:
That's my plain, too.
| } | ||
| // We expect at this point that all references have been attempted to resolve. | ||
| if !r.resolved.Load() { | ||
| return nil, errors.New("reference not resolved") |
There was a problem hiding this comment.
This is just preliminary, I don't know which context instance to rely on for calling Ref(...), yet
(since this function is called by json.Mashall(...), not by ourselves), and whether we should attempt to resolve here, at all.
AFAICS, r.resolved is true if resolution has been attempted, regardless of success or failure.
Hence, this guards stops the serialization, e.g., if the document hasn't been build until DocStateLinked at least.
23336ec to
10ec511
Compare
66a1bfa to
f72abbc
Compare
…lementations * also added generic 'MarshalJSON' & 'UnmarshalJSON' implementation for 'Reference'
f72abbc to
cc2a141
Compare
cc2a141 to
8ca6a6c
Compare
Contributes support for exporting and importing ASTs in Json.
Includes:
a generator component producing
json_gen.gofilesjson.Marshaler&json.Unmarshalergeneric
MarshalJSON&UnmarshalJSONimplementations forReferenceutil/json/json.godemonstrates the procedure of importing an AST in json into a Fastbelt containera simple round trip AST export and import test including cross references to other documents (circular) by means of the Arithmetics test language,
a roundtrip AST export and import test (no external cross-refs by now) by means of the Grammar language,
boolean,string,AstNode,Referencex1,?,*,+Note:
fixture.gonilpointer de-reference insymbol.goThere's one known potential issue:
During the import of an AST in json the
Tokeninstances are always initialized with token typeToken_ID.The leads to a compiler issue if there is no
IDtoken type.We could improve this by an analysis of the Parser rules, but that will never be complete.
Imagine the grammar defines interfaces of types that are never instantiated by parser rules, but the data are always imported vom Json snapshots.
To mitigate this we might offer an opportunity in the grammar to determine a token type for fields of type
stringorcomposite. To be discussed.