1.0.9 • Published 1 year ago

json2-go v1.0.9

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

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":"admin@rogue-syntax.com"}
	
]
  • 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:

admin@rogue-syntax.com:~$ npm install -g json2-go
admin@rogue-syntax.com:~$ npm exec json2-go --file=./somefile.json --out-./ --name:MyType

Install from github and use with npm:

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

Install from github and use with nodejs

admin@rogue-syntax.com:~$ git clone https://github.com/rogue-syntax/json2-go.git
admin@rogue-syntax.com:~$ cd json2-go
admin@rogue-syntax.com:~/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
}
1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

0.0.0

1 year ago