all repos

json2go @ 917fe252d3d54a71e05b02de24f4d4d361cb21ef

convert json to go type annotations
1 files changed, 59 insertions(+), 4 deletions(-)
implement the cli
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2025-11-26 22:31:35 +0200
Authored at: 2025-11-26 21:15:03 +0200
Change ID: lwpmvoswtrwxpmqpwzwrpvmxsspkxvvz
Parent: 25c3e78
M cmd/json2go/main.go
ยทยทยท
        1
        1
         package main

      
        2
        2
         

      
        3
        
        -// todo: piped / sedin

      
        4
        
        -// todo: passed as argument

      
        5
        
        -// todo: read from file

      
        6
        
        -// todo: set name of the type

      
        
        3
        +import (

      
        
        4
        +	"flag"

      
        
        5
        +	"fmt"

      
        
        6
        +	"io"

      
        
        7
        +	"os"

      
        
        8
        +

      
        
        9
        +	"olexsmir.xyz/json2go"

      
        
        10
        +)

      
        7
        11
         

      
        8
        12
         func main() {

      
        
        13
        +	typeName := flag.String("type", "AutoGenerated", "a name for generated type")

      
        
        14
        +	showHelp := flag.Bool("help", false, "show help")

      
        
        15
        +	flag.Parse()

      
        
        16
        +

      
        
        17
        +	if *showHelp {

      
        
        18
        +		printHelp()

      
        
        19
        +		os.Exit(0)

      
        
        20
        +	}

      
        
        21
        +

      
        
        22
        +	// get input

      
        
        23
        +	args := flag.Args()

      
        
        24
        +

      
        
        25
        +	stat, err := os.Stdin.Stat()

      
        
        26
        +	if err != nil {

      
        
        27
        +		panic(err)

      
        
        28
        +	}

      
        
        29
        +

      
        
        30
        +	isPiped := (stat.Mode() & os.ModeCharDevice) == 0

      
        
        31
        +

      
        
        32
        +	var input string

      
        
        33
        +	switch {

      
        
        34
        +	case len(args) > 0:

      
        
        35
        +		input = args[0]

      
        
        36
        +	case isPiped:

      
        
        37
        +		data, rerr := io.ReadAll(os.Stdin)

      
        
        38
        +		if rerr != nil {

      
        
        39
        +			panic(rerr)

      
        
        40
        +		}

      
        
        41
        +		input = string(data)

      
        
        42
        +	default:

      
        
        43
        +		printHelp()

      
        
        44
        +		os.Exit(1)

      
        
        45
        +	}

      
        
        46
        +

      
        
        47
        +	transformer := json2go.NewTransformer()

      
        
        48
        +	type_, err := transformer.Transform(*typeName, input)

      
        
        49
        +	if err != nil {

      
        
        50
        +		panic(err)

      
        
        51
        +	}

      
        
        52
        +

      
        
        53
        +	fmt.Println(type_)

      
        
        54
        +}

      
        
        55
        +

      
        
        56
        +func printHelp() {

      
        
        57
        +	fmt.Println(`

      
        
        58
        +Convert json to Go type annotations.

      
        
        59
        +

      
        
        60
        +Usage:

      
        
        61
        +	echo '{"json": "here"}' | json2go

      
        
        62
        +	echo '{"json": "here"}' | json2go -type=MyTypeName

      
        
        63
        +	json2go -type=MyTypeName '{"json": "here"}'`[1:])

      
        9
        64
         }