Giter Site home page Giter Site logo

Comments (7)

aren55555 avatar aren55555 commented on August 24, 2024

@iderdik I'd suggest using pointers for fields that permit nil values.

type Car struct {
  ...
  PurchasedAt *time.Time `jsonapi:"attr,purchased_at"`
  ...
}

from jsonapi.

iderdik avatar iderdik commented on August 24, 2024

Thanks, that should work.

from jsonapi.

iderdik avatar iderdik commented on August 24, 2024

After changing the struct members to pointers, I now get:

data is not a jsonapi representation of '*models.xxxx'"

I haven't changed anything else about the struct. Are pointers handle differently?

from jsonapi.

aren55555 avatar aren55555 commented on August 24, 2024

@iderdik can you post an example of the struct you are trying to serialize/deserialize?

from jsonapi.

iderdik avatar iderdik commented on August 24, 2024

@aren55555 Further research shows that this error happens when there are *string members in the struct. This is the line that panics: https://github.com/shwoodard/jsonapi/blob/master/request.go#L312

It looks like there might be a need for a check if reflect.Kind() is a pointer and handle appropriately.

This code in that spot made it work for me:

if fieldValue.Kind() == reflect.Ptr {
  strval := val.(string)
  fieldValue.Set(reflect.ValueOf(&strval))
} else {
  fieldValue.Set(reflect.ValueOf(val))
}

Of course it assumes that the value is *string so you still might need a default case handler.

from jsonapi.

iderdik avatar iderdik commented on August 24, 2024

And this all seems to be dealt with in this PR :) https://github.com/shwoodard/jsonapi/pull/28 Any chance that will get merged soon?

from jsonapi.

aren55555 avatar aren55555 commented on August 24, 2024

@iderdik master now has the commits that support fields of *string

from jsonapi.

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.