jsonhandlers

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2023 License: MIT Imports: 5 Imported by: 0

README

JsonHandlers

Json Handlers  

JSON library to expose simple handlers that lets you easily read and write json from various sources.

Build Status Github top language Github language count License Go Report Card Go Reference Repo size Coverage Status Mentioned in Awesome Go

Prerequisites

A go module where you want to integrate jsonhandlers. To create one, follow this guide.

Installation

go get github.com/abusomani/jsonhandlers

Usage

A very useful feature of Go’s import statement are aliases. A common use case for import aliases is to provide a shorter alternative to a library’s package name.

In this example, we save ourselves having to type jsonhandlers everytime we want to call one of the library’s functions, we just use jh instead.

import (
    jh "github.com/abusomani/jsonhandlers"
)

Options

Jsonhandlers package exposes multiple options while creating a new jsonhandler to be able to read/write json from sources like Files, Http Requests or Http responses.

WithFileHandler

You can use the WithFileHandler option to read/write Json from/to a file. For this, you need to create a new jsonhandler with the file handler option.

Example to understand WithFileHandler in more detail.

Sample Code

package operations

import (
	"fmt"

	"github.com/abusomani/jsonhandlers"
)

func GetStudentsFromFile() []student {
	return handleFile()
}

func handleFile() []student {
	jh := jsonhandlers.New(jsonhandlers.WithFileHandler(testFilePath))

	var sch school
	err := jh.Unmarshal(&sch)
	handleError("error in unmarshalling %s", err)
	fmt.Printf("School info is : %+v\n", sch)

	// add a new student to the school
	sch.Students = append(sch.Students[:2], student{
		Id:     3,
		Name:   "The new student",
		Branch: "AI",
	})

	err = jh.Marshal(sch)
	handleError("error in marshalling %s", err)
	fmt.Printf("Updated school info after admission of new student is : %+v\n", sch)

	// remove the new student as he was very mischievous
	sch.Students = sch.Students[:2]

	err = jh.Marshal(sch)
	handleError("error in marshalling %s", err)
	fmt.Printf("Updated school info after retaining all good students is : %+v\n", sch)
	return sch.Students
}

WithHTTPRequestHandler

You can use the WithHTTPRequestHandler option to read Json from a Http Request and to write Json to a Http ResponseWriter. For this, you need to create a new jsonhandler with the Http request handler option.

Example to understand WithHTTPRequestHandler in more detail.

Sample Code

package operations

import (
	"net/http"

	"github.com/abusomani/jsonhandlers"
)

type studentSearchRequest struct {
	Name string
}

type studentSearchResponse struct {
	Info student
}

func HandleHTTPRequest(students []student) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		
    jh := jsonhandlers.New(jsonhandlers.WithHTTPRequestHandler(w, r))

		var reqBody studentSearchRequest
		err := jh.Unmarshal(&reqBody)
		if err != nil {
			errPayload := struct {
				StatusCode int
				Message    string
			}{
				StatusCode: http.StatusBadRequest,
				Message:    err.Error(),
			}
			// request is bad
			jh.Marshal(errPayload)
			return
		}

		for _, student := range students {
			// student found
			if student.Name == reqBody.Name {
				// response has the right student info written
				jh.Marshal(studentSearchResponse{
					Info: student,
				})
				return
			}
		}

		errPayload := struct {
			StatusCode int
			Message    string
		}{
			StatusCode: http.StatusInternalServerError,
			Message:    "something went wrong",
		}
		// student not found
		jh.Marshal(errPayload)
	})
}

/*
  Sample request to be hit on the localhost server to test WithHTTPRequestHandler functionality.
  curl http://localhost:8080/search -d '{"Name": "Abhishek Somani"}'
*/

WithHTTPResponseHandler

You can use the WithHTTPResponseHandler option to read/write Json from/to a Http Response. For this, you need to create a new jsonhandler with the Http response handler option.

Example to understand WithHTTPResponseHandler in more detail.

Sample Code

package operations

import (
	"fmt"
	"log"
	"net/http"

	"github.com/abusomani/jsonhandlers"
)

type user struct {
	Id        int
	FirstName string
	LastName  string
	Age       int
	Gender    string
	Email     string
}

type getUsersResponse struct {
	Users []user
}

func HandleHTTPResponse() {
	resp, err := http.Get("https://dummyjson.com/users")
	if err != nil {
		log.Fatalf("unable to make the get request %s", err.Error())
	}
	jh := jsonhandlers.New(jsonhandlers.WithHTTPResponseHandler(resp))

	var userResp getUsersResponse
	jh.Unmarshal(&userResp)
	fmt.Printf("response is %+v\n", userResp)
}

Run examples

To run the examples present in the example folder you need to first checkout this package by doing a git clone. Once you have checked out this package, then you can run the main.go using the following command to see all the examples in action:

go run example/main.go

License

Licensed under MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONHandler

type JSONHandler struct {
	// contains filtered or unexported fields
}

JSONHandler exposes Marshal and Unmarshal methods.

func New

func New(opts ...Option) *JSONHandler

New returns a new JSONHandler instance with variable options to set the required configurations.

func (*JSONHandler) Marshal

func (jh *JSONHandler) Marshal(v any) error

Marshal writes the JSON encoding of v in the configured source of handler.

JSON cannot represent cyclic data structures and Marshal does not handle them. Passing cyclic structures to Marshal will result in an error.

func (*JSONHandler) SetOptions

func (jh *JSONHandler) SetOptions(opts ...Option) *JSONHandler

func (*JSONHandler) Unmarshal

func (jh *JSONHandler) Unmarshal(v any) error

Unmarshal parses the JSON-encoded data and stores the result in the value given in input as `v`.

type Option

type Option func(*JSONHandler)

Option is a self-referential function to make palette modular and extensible. Read more about self-referential function here: https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html

func WithDefaults

func WithDefaults() Option

WithDefaults is a self-referential function. It sets the NoopHandler on the JSONHandler receiver.

func WithFileHandler

func WithFileHandler(fileName string) Option

WithFileHandler is a self-referential function that takes in a fileName It sets the handler as FileHandler on the JSONHandler receiver.

func WithHTTPRequestHandler

func WithHTTPRequestHandler(rw http.ResponseWriter, req *http.Request) Option

WithHTTPRequestHandler is a self-referential function that takes in a http.ResponseWriter and a pointer to http.Request It sets the handler as HTTPRequestHandler on the JSONHandler receiver.

func WithHTTPResponseHandler

func WithHTTPResponseHandler(res *http.Response) Option

WithHTTPResponseHandler is a self-referential function that takes in a pointer to http.Response It sets the handler as HTTPResponseHandler on the JSONHandler receiver.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL