npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json2-go

v1.0.9

Published

A convenience command line tool to translate an mysql eported row in json format into a GO struct and scan function

Downloads

2

Readme

JSON2-Go

'json2-go' is a convenience command line tool for taking a json formatted mysql row result and generating a GO struct and row scan function.

  • Useful when you have a joined view with many colunms that keeps changing around in the course of developemnt

Why?

I was tired of having to update my struct definintions and sql row scanning functions for large, many columned mysql / mariadb joined views:

  • When changing joining logic would cause a reordering of lots of columns / struct feilds
  • When adding / removing / altering columns and column types
  • etc.

Caveat

This utility only works with non null column values to avoid the issue of scanning sql null values into go primatives.

How

Many mysql/mariadb IDE programs will help you export a JSON serialization of a result set for a large, complex joined table or view selection result. Export one row with all columns holding non null data to somefile.json:

[
	{"user_id":"1","email_id":"1", "email_id":"1", "email_value":"[email protected]"}
	
]
  • json2-go uses the Clarinet.js stream parser to mitigate and preserve duplicate column names which are incompatable with JSON.parse
  • so multiple rows will result in duplicate fields for every row
  • so, just export one row with all columns holding non null data to somefile.json

Install from npm and use with npm:

[email protected]:~$ npm install -g json2-go
[email protected]:~$ npm exec json2-go --file=./somefile.json --out-./ --name:MyType

Install from github and use with npm:

[email protected]:~$ git clone https://github.com/rogue-syntax/json2-go.git
[email protected]:~$ cd json2-go
[email protected]:~/json2-go$ npm run j2g --file=./somefile.json --out-./ --name:MyType

Install from github and use with nodejs

[email protected]:~$ git clone https://github.com/rogue-syntax/json2-go.git
[email protected]:~$ cd json2-go
[email protected]:~/json2-go$ nodejs json2go.js --file=./somefile.json --out-./ --name:MyType

Duplicate column names from join

If you have a join that results in duplicate column names, json2go will flag and generate duplicate struct fields:
- use optional argument --duplicates, otherwise dupicates will be omitted from type definition (while still being mitigated from incoming sql rows in the scanner)
- output type with duplicates:

Command Line Arguments

  • --file= Path to your json file
  • --name= Name for the type definition
  • --out= Path to outut to
  • --duplicates= Optional flag to include duplicate fields in type definition
type User struct { 
	User_id  string 
	Email_id  string 
	email_id_DUPLICATE_0  string 
	Email_value  string 
}
  • output scanner with duplicates:
func ScanUser( ul *User, rows * sql.Rows) error { 
	var duplicateInt int 
	var duplicateFloat64 float64 
	var duplicateString int 
	_ = duplicateInt 
	_ = duplicateFloat64 
	_ = duplicateString 
	scanErr := rows.Scan(&ul.User_id, 
	&ul.Email_id, 
	&duplicateString, 
	&ul.Email_value, 
	)
	if scanErr != nil {
		return scanErr
	}
	return nil
}