gohelpertools

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2023 License: MIT Imports: 8 Imported by: 0

README

go-helper-tools

A collection of helper functions I've had to use in different projects

Version Built with GoLang

Helper Tools

The included tools are:

  • Read JSON
  • Write JSON
  • Get a random string of length n
  • Create a URL safe slug from a string

Installation

go get -u github.com/oluwaferanmiadetunji/go-helper-tools

Usage

package main

import (
	"fmt"
	"github.com/oluwaferanmiadetunji/go-helper-tools"
)

func main() {
	// create a variable of type gohelpertools.Tools, so we can use this variable
	// to call the methods on that type
	var tools gohelpertools.Tools

	// get a random string
	randomString := tools.RandomString(10)
	fmt.Println(randomString)
}
Working with JSON

In a handler, for example:

// JSONPayload is the type for JSON data that we receive
type JSONPayload struct {
    Name string `json:"name"`
    Data string `json:"data"`
}

// SomeHandler is the handler to accept a post request consisting of json payload
func (app *Config) SomeHandler(w http.ResponseWriter, r *http.Request) {
    var tools gohelpertools.Tools
    
    // read json into var
    var requestPayload JSONPayload
    _ = tools.ReadJSON(w, r, &requestPayload)
	
    // do something with the data here...
    
    // create the response we'll send back as JSON
    resp := gohelpertools.JSONResponse{
        Error:   false,
        Message: "logged",
    }
    
    // write the response back as JSON
    _ = tools.WriteJSON(w, http.StatusAccepted, resp)
}
Create a slug from a string

To slugify a string, we simply remove all non URL safe characters and return the original string with a hyphen where spaces would be. Example:

package main

import (
	"fmt"
	"github.com/oluwaferanmiadetunji/go-helper-tools"
)

func main() {
	toSlugify := "hello, world! These are unsafe chars: こんにちは世界*!&^%"
	fmt.Println("To slugify:", toSlugify)
	var tools gohelpertools.Tools

	slug, err := tools.Slugify(toSlugify)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("Slugified:", slug)
}

Output from this is:

To slugify: hello, world! These are unsafe chars: こんにちは世界*!&^%
Slugified: hello-world-these-are-unsafe-chars

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONResponse

type JSONResponse struct {
	Error   bool   `json:"error"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

type Tools

type Tools struct {
	MaxJSONSize        int  // maximum size of JSON file we'll process
	AllowUnknownFields bool // if set to true, allow unknown fields in JSON
}

func (*Tools) ErrorJSON

func (t *Tools) ErrorJSON(w http.ResponseWriter, err error, status ...int) error

ErrorJSON takes an error, and optionally a response status code, and generates and sends a JSON error response.

func (*Tools) RandomString

func (t *Tools) RandomString(n int) string

RandomString returns a random string of letters of length n, using characters specified in randomStringSource.

func (*Tools) ReadJSON

func (t *Tools) ReadJSON(w http.ResponseWriter, r *http.Request, data any) error

ReadJSON tries to read the body of a request and converts it from JSON to a variable. The third parameter, data, is expected to be a pointer, so that we can read data into it.

func (*Tools) Slugify

func (t *Tools) Slugify(s string) (string, error)

Slugify is a (very) simple means of creating a slug from a provided string.

func (*Tools) WriteJSON

func (t *Tools) WriteJSON(w http.ResponseWriter, status int, data interface{}, headers ...http.Header) error

WriteJSON takes a response status code and arbitrary data and writes a JSON response to the client.

Jump to

Keyboard shortcuts

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