📅 You're Invited: Meet the Socket team at RSAC (April 28 – May 1).RSVP
Socket
Sign inDemoInstall
Socket

github.com/aliforever/go-generichandler

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/aliforever/go-generichandler

v0.0.5
Source
Go
Version published
Created
Source

go-generichandler

A simple json event handler using generics made to save hours of my life in tens of projects

Get

go get -u github.com/aliforever/go-generichandler

Example:

package main

import (
	"fmt"
	"github.com/aliforever/go-generichandler"
	"time"
)

type ping struct {
	Time time.Time `json:"time"`
}

func PingHandler(p ping) error {
	fmt.Println(p.Time)
	return nil
}

func main() {
	handlers := generichandler.NewHandlers()

	handlers.AddHandler("ping", generichandler.Handler[ping](PingHandler))
    
	event := []byte(fmt.Sprintf(`{"time": "%s"}`, time.Now().Format(time.RFC3339)))

	err := handlers.Handle("ping", event)
	if err != nil {
		panic(err)
	}
}

Basically I follow a convention in my projects, each handler has an input of type []byte (json.RawMessage).

With this package we remove the need to decode json types inside handler functions to their specific type

FAQs

Package last updated on 23 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts