Git Product home page Git Product logo

Comments (6)

tj avatar tj commented on June 4, 2024

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.

tj avatar tj commented on June 4, 2024

370c365 thanks!

from rpc.

tj avatar tj commented on June 4, 2024

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.

kklas avatar kklas commented on June 4, 2024

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.

tj avatar tj commented on June 4, 2024

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.

kklas avatar kklas commented on June 4, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.