codec

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 3 Imported by: 0

README

gin-codec

Instead of Bind and Render, call Decode and encode for custom case




install

go get github.com/FPNL/codec

usage example

package main

import (
	"github.com/FPNL/codec"
	"github.com/gin-gonic/gin"
)

func handler(coder codec.Codec) gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var in any
		var out any

		// decode
		if err := coder.Bind(ctx, &in); err != nil {
			_ = ctx.Error(err)
			return
		}

		// decode
		if err := coder.BindQuery(ctx, &in); err != nil {
			_ = ctx.Error(err)
			return
		}

		// decode
		if err := coder.BindVars(ctx, &in); err != nil {
			_ = ctx.Error(err)
			return
		}

		// Do what you want to in

		// encode
		err := coder.Result(ctx, out)
		if err != nil {
			_ = ctx.Error(err)
			return
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec interface {
	Bind(*gin.Context, any) error
	BindVars(*gin.Context, any) error
	BindQuery(*gin.Context, any) error
	BindForm(*gin.Context, any) error
	Result(*gin.Context, any) error
}
var DefaultCodec Codec = &codec{}

Jump to

Keyboard shortcuts

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