Git Product home page Git Product logo

Comments (4)

lpongetti avatar lpongetti commented on May 5, 2024

ok i found.
i done this

type Users []*User
func (users Users) JSONAPILinks() *jsonapi.Links {
	return &jsonapi.Links{
		"self": "/api/v1/users",
	}
}

now i don't know how can i do the same thing with a single struc (*User).
And how can i take meta and links when i unmarshal?

Sorry but i'm new in Go
Thanks.

from jsonapi.

aren55555 avatar aren55555 commented on May 5, 2024

This should help you:

main.go:

package main

import (
	"bytes"
	"fmt"

	"github.com/google/jsonapi"
)

type User struct {
	ID      int    `jsonapi:"primary,users"`
	Code    string `jsonapi:"attr,code"`
	Name    string `jsonapi:"attr,name"`
	Surname string `jsonapi:"attr,surname"`
}

func (user User) JSONAPILinks() *jsonapi.Links {
	return &jsonapi.Links{
		"self": fmt.Sprintf("/api/v1/users/%d", user.ID),
	}
}

type Users []*User

func (users Users) JSONAPILinks() *jsonapi.Links {
	return &jsonapi.Links{
		"self": "/api/v1/users",
	}
}

func main() {
	u1 := &User{ID: 1, Name: "aren55555"}
	u2 := &User{ID: 2, Name: "lpongetti"}
	users := Users{u1, u2}

	s1, s2, s3 := bytes.NewBuffer(nil), bytes.NewBuffer(nil), bytes.NewBuffer(nil)

	// Serialize Individual Users
	jsonapi.MarshalPayload(s1, u1)
	fmt.Println(s1.String())
	jsonapi.MarshalPayload(s2, u2)
	fmt.Println(s2.String())

	// Serilalize a Collection of Users
	jsonapi.MarshalPayload(s3, users)
	fmt.Println(s3.String())
}

Outputs 3 JSON blobs:

{
    "data": {
        "type": "users",
        "id": "1",
        "attributes": {
            "code": "",
            "name": "aren55555",
            "surname": ""
        },
        "links": {
            "self": "/api/v1/users/1"
        }
    }
}
{
    "data": {
        "type": "users",
        "id": "2",
        "attributes": {
            "code": "",
            "name": "lpongetti",
            "surname": ""
        },
        "links": {
            "self": "/api/v1/users/2"
        }
    }
}
{
    "data": [{
        "type": "users",
        "id": "1",
        "attributes": {
            "code": "",
            "name": "aren55555",
            "surname": ""
        },
        "links": {
            "self": "/api/v1/users/1"
        }
    }, {
        "type": "users",
        "id": "2",
        "attributes": {
            "code": "",
            "name": "lpongetti",
            "surname": ""
        },
        "links": {
            "self": "/api/v1/users/2"
        }
    }],
    "links": {
        "self": "/api/v1/users"
    }
}

from jsonapi.

imhuytq avatar imhuytq commented on May 5, 2024

@aren55555 How can I add self, first, prev, next, last page links (with page[number]) to Links on root level?

from jsonapi.

aren55555 avatar aren55555 commented on May 5, 2024

@pp4p see #93 (comment)

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.