Closed JounQin closed 2 years ago
You mean as exported Go APIs, since currently -tojson
is not?
You mean as exported Go APIs, since currently
-tojson
is not?
Right, I need them in sh-syntax
.
A notice for WASM, we'd better not use reflect
to generate json
, because it could be broken, I'm using easyjson
at sh-syntax
, see https://github.com/un-ts/sh-syntax/blob/main/processor/structs_easyjson.go
At some point I do want to remove the dependency on reflect, but right now I want to stick with encoding/json
for the sake of not pulling in large or buggy third-party json implementations. You're of course free to use your own implementation.
You're of course free to use your own implementation.
I'm not sure how to. 😅
sh-syntax
depends on this go library, if it depends on reflect
in ToJson
, how can I override it? Provide another copy on my side? Would it be hard to maintain?
Sorry I'm very new bee for go lang. Could you help to give me some advices?
Yes, I indeed mean not using these APIs if you cannot use reflect. So you probably shouldn't wait for them right now :)
I should also add that we should look into why our use of reflect wouldn't work. I am fairly sure that TinyGo does support part of reflect, for example.
Yep, partly supported.
And we should also consider the final bundle size for WASM, that why we use tinygo
, right?
Right, size matters. That's a reason to not use libraries like easyjson, in fact, because they are often many times bigger than encoding/json
:)
encoding/json
+ reflect
is not fully supported by tinygo
, so I have to use easyjson
instead. 🤣
I tried but failed with native encoding/json
+ reflect
with tinygo
.
easyjson
replaces reflect
in this case.
Looks like it is only blocked on reflect.StructOf
(https://tinygo.org/docs/reference/lang-support/stdlib/#encodingjson), which I'm also using for the new version of "to JSON", since Go maps aren't ordered. I've left a comment at https://github.com/tinygo-org/tinygo/issues/2115#issuecomment-1183304339.
easyjson
replacesreflect
in this case.
Right, though easyjson uses code generation, so I'm fairly sure that the output size suffers because of it.
In the future, I can remove the use of reflection by swapping encoding/json with the token encoder and decoder in https://github.com/go-json-experiment/json. I don't want to do that right away, as I want to get "from JSON" working well first.
Great to hear!
Thinking outloud: this API should not be in the syntax package, because we don't want to require a JSON library to be able to parse shell.
Thinking outloud: this API should not be in the syntax package, because we don't want to require a JSON library to be able to parse shell.
What do you think should we do in sh-syntax
package then?
Nothing has to change there. It's just that instead of having the API like ToJSON(io.Writer, syntax.Node)
in the mvdan.cc/sh/v3/syntax
package, I'll probably put it somewhere else like mvdan.cc/sh/v3/syntax/typedjson
. You can use both packages in your wasm code.
via https://github.com/mvdan/sh/issues/760#issuecomment-1120420888
sh-syntax
(WASM) is about 2x faster thanmvdan-sh
https://github.com/rx-ts/sh-syntax/blob/main/benchmark/benchmark.txt