Comments (6)
somehow I missed that haha, yeah I think we should use whatever is idiomatic for the target language, so SGTM, I'll fix that today
from rpc.
370c365 thanks!
from rpc.
oh actually I just remembered why it's this way, because TS will serialize the fields incorrectly. If fields are defined as project_id
in the schema, then you get projectId
since — as far as I know — there's no easy way to map JSON property names in TS like there is in Go for example.
from rpc.
because TS will serialize the fields incorrectly. If fields are defined as project_id in the schema, then you get projectId
Not sure what you mean here. Looked at the ts client code and this is what I understand is happening:
- canonically we use snake_case in the schema
- we JSON encode field names to snake_case server side
- ts client receives and parses the objects as is
- types match the snake_case fields in the underlying object so everything works at run time
there's no easy way to map JSON property names in TS like there is in Go for example
Yea I guess we can't do this within JSON.parse reviver but shouldn't it be straightforward to recurse on the object fields and create a new object with camel case?
from rpc.
It's definitely doable to recurse, but I'm not sure it's really worth the pain, I don't mind the snake-case much personally but I don't use any linters. If there is a clean way to do it in TS I'm definitely cool with it, but keep in mind it has to work for regular JS as well as TS.
I think we can maybe use the JSON.stringify/parse callbacks, looks like we'd have to return undefined
in stringify
so it doesn't add the value, and then this[camelCased] = value
.
from rpc.
Played a bit with JSON.parse callback but couldn't get it to work on firefox:
function camelize(str) {
capitalize = (str) => {str.charAt(0).toUpperCase() + str.slice(1)}
tok = str.split("_")
ret = tok[0]
tok.slice(1).forEach((t) => ret += capitalize(t))
return ret
}
JSON.parse(o, (key, value) => {
this[camelize(key)] = value
delete(this[key])
})
I guess if you return undefined it just throws the whole thing away? Maybe I missed something.
This seems a bit hacky anyways and I guess it would also depend on JSON.parse/stringify implementation in the engine so I wouldn't go for this approach.
I'll do a PR so let's see how it can be implemented.
from rpc.
Related Issues (20)
- Add the schema's version to the output
- Target all JavaScript platforms with one generator HOT 5
- Move rpc-apex-docs
- Generate Terraform providers
- Improved validation error messages
- Unions for response types to include errors HOT 9
- Generate CLIs
- timestamp fields get parsed as string in the ts client HOT 13
- Mock server for testing
- Generate Go example for docs
- [TS client] string fields containing timestamps get parsed as Date objects
- Add "since" schema property for the version introducing a type/method
- Swift client
- Gleam client
- Dotnet client
- Elixir client
- Clojure client HOT 1
- Scala client
- Crystal client
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rpc.