Git Product home page Git Product logo

Comments (3)

gnewton avatar gnewton commented on May 5, 2024 2

The information that is needed to do pagination (offset, limit) plus meta needs to be made available inside of the Linkable/Meta which means the implementer of LInkable/Meta needs to store it somewhere. For a Person struct it can be in internal fields that are not marshalled:


type Person struct{
	ID        int64  `jsonapi:"primary,taxon"`
	Name      string `jsonapi:"attr,name,omitempty"`
	metainfo  string 
}

But you cannot attach these kinds of fields to a []Person.

I would prefer changing the APIs (!!!) to be:

func MarshalPayload(w io.Writer, models interface{}, extras interface{}) error

type Linkable interface {
    JSONAPILinks(extras interface{}) *Links
}

type Metable interface {
    JSONAPIMeta(extras interface{}) *Meta
}

from jsonapi.

svperfecta avatar svperfecta commented on May 5, 2024

We used to do this by manually calling MarshalOne and MarshaMany, getting back the data object, manually adding links meta, and then json encoding the resulting payload. Bit annoying, but it was the only way to do top level (non object specific) links and meta. The library changed quite a bit, so I'm not sure (yet) how we'll do it now.

from jsonapi.

svperfecta avatar svperfecta commented on May 5, 2024

I updated our original code today. Essentially we wrote a wrapper for this library with some helper functions. We implemented a MarshalMany function (which I can probably combine with our MarshalOne function, since the library handles slices vs ptrs internally now). Anyways, I think you want something like this:

//MarshalMany marshals many item payloads
func (p *Payload) MarshalMany(writer io.Writer) error {

	i, err := convertToSliceInterface(&p.Data)
	if err != nil {
		return fmt.Errorf(" Internal Server Error")
	}

	payload, err := jsonapi.Marshal(i)
	if err != nil {
		return fmt.Errorf(" Internal Server Error")
	}

	payload.(*jsonapi.ManyPayload).Links = &p.Links.Links
	payload.(*jsonapi.ManyPayload).Meta = &p.Meta.Meta

	json.NewEncoder(writer).Encode(payload)
	return nil
}

There's probably a nicer way to do this, but the problem is that you need a way to pass something more complex than a slice or a ptr to MarshalPayload. Ideally that itself could be an object, that looks a lot like the OnePayload object.

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.