stringify

package module
v0.0.0-...-be691d5 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-stringify

Build Status

Stringify function for golang, it just like javascript's JSON.stringify. It supports customized replacement function, just like converting password to "***", cutting the string of which length is longer than 30.

API

String(interface{}, Replacer)

  • interface{} data to json stringify
  • Replacer function for customized replacement
Replacer func(key string, value interface{}) (replace bool, newValue string)
package main

import (
	"fmt"

	stringify "github.com/vicanso/go-stringify"
)

type (
	Other struct {
		Name        string                 `json:"name,omitempty"`
		Age         int                    `json:"age,omitempty"`
		VIP         bool                   `json:"vip,omitempty"`
		Information map[string]interface{} `json:"information,omitempty"`
	}
	Me struct {
		ID          string                 `json:"id,omitempty"`
		Account     string                 `json:"account,omitempty"`
		Password    string                 `json:"password,omitempty"`
		Secret      string                 `json:"-"`
		Name        string                 `json:"name,omitempty"`
		Age         int                    `json:"age,omitempty"`
		VIP         bool                   `json:"vip,omitempty"`
		Detail      string                 `json:"detail,omitempty"`
		Information map[string]interface{} `json:"information,omitempty"`
		Boss        Other                  `json:"boss,omitempty"`
		Leader      *Other                 `json:"leader,omitempty"`
		Workmates   []Other                `json:"workmates,omitempty"`
		Friends     []*Other               `json:"friends,omitempty"`
		Nil         []*Other               `json:"nil,omitempty"`
	}
)

func main() {
	s := getData()
	// {"account":"t***t","password":"***","name":"foo","age":18,"detail":"GitHub is where people build s...","information":{"male":true,"bestFriend":{"name":"peter"},"fav":"reading","weight":70},"boss":{"name":"boss"},"leader":{"age":30},"workmates":[{"name":"jack"},{"name":"tas"}],"friends":[{"name":"tom"}]}
	fmt.Println(stringify.String(s, replacer))
}

func replacer(key string, value interface{}) (bool, string) {
	// password is hidden by ***
	if key == "password" {
		return true, `"***"`
	}
	// mask the account
	if key == "account" {
		v := value.(string)
		return true, `"` + v[0:1] + "***" + v[len(v)-1:] + `"`
	}
	str, ok := value.(string)
	// cut the string
	if ok && len(str) > 30 {
		return true, `"` + str[0:30] + `..."`
	}
	// others not change
	return false, ""
}

func getData() *Me {
	friends := make([]*Other, 2)
	friends = append(friends, &Other{
		Name: "tom",
	})
	peter := &Other{
		Name: "peter",
	}
	return &Me{
		ID:       "",
		Account:  "test",
		Password: "pwd",
		Secret:   "none",
		Name:     "foo",
		Age:      18,
		VIP:      false,
		Detail:   "GitHub is where people build software. More than 31 million people use GitHub to discover, fork, and contribute to over 100 million projects.",
		Information: map[string]interface{}{
			"fav":        "reading",
			"weight":     70,
			"male":       true,
			"bestFriend": peter,
		},
		Boss: Other{
			Name: "boss",
		},
		Leader: &Other{
			Age: 30,
		},
		Workmates: []Other{
			Other{
				Name: "jack",
			},
			Other{
				Name: "tas",
			},
		},
		Friends: friends,
	}
}

benchmark

goos: darwin
goarch: amd64
pkg: github.com/vicanso/go-stringify
BenchmarkStringify-4   	   50000	     23350 ns/op	    3320 B/op	     144 allocs/op
BenchmarkMarshal-4     	  300000	      5548 ns/op	    1504 B/op	      16 allocs/op
PASS
ok  	github.com/vicanso/go-stringify	3.146s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBufferSize

func GetBufferSize() int

GetBufferSize get initialize buffer size

func SetBufferSize

func SetBufferSize(size int)

SetBufferSize set initialize buffer size

func String

func String(s interface{}, replacer Replacer) string

String stringify

Types

type JSONStringify

type JSONStringify struct {
	Sb       *bytes.Buffer
	TagName  string
	Replacer Replacer
}

JSONStringify json stringify

func (*JSONStringify) Array

func (js *JSONStringify) Array(s interface{})

Array stringify array

func (*JSONStringify) Map

func (js *JSONStringify) Map(s interface{})

Map stringify map

func (*JSONStringify) St

func (js *JSONStringify) St(s interface{})

St stringify struct

func (*JSONStringify) String

func (js *JSONStringify) String(s interface{}) string

String json stringify

type Replacer

type Replacer func(key string, value interface{}) (replace bool, newValue string)

Replacer replace function

Jump to

Keyboard shortcuts

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