xj2go

package module
v0.0.0-...-4042c1b Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2018 License: MIT Imports: 15 Imported by: 0

README

xj2go

Go Report Card Build Status codecov codebeat badge

The goal is to convert xml or json file to go struct file.

Usage

Download and install it:

$ go get -u -v github.com/stackerzzq/xj2go/cmd/...

$ xj [-t json/xml] [-p sample] [-r result] sample.json

Import it in your code:

import "github.com/stackerzzq/xj2go"

Example

Please see the example file.

package main

import (
	"io/ioutil"
	"log"

	"github.com/stackerzzq/xj2go"
)

func main() {
	xmlfilename := "../testxml/xl/styles.xml"
	xj1 := xj2go.New(xmlfilename, "demoxml", "")
	xj1.XMLToGo()

	b1, err := ioutil.ReadFile(xmlfilename)
	if err != nil {
		log.Fatal(err)
	}

	if err := xj2go.XMLBytesToGo("test", "demoxml2", &b1); err != nil {
		log.Fatal(err)
	}

	jsonfilename := "../testjson/githubAPI.json"
	xj2 := xj2go.New(jsonfilename, "demojson", "sample")
	xj2.JSONToGo()

	b2, err := ioutil.ReadFile(jsonfilename)
	if err != nil {
		log.Fatal(err)
	}

	if err := xj2go.JSONBytesToGo("test", "demojson2", "github", &b2); err != nil {
		log.Fatal(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONBytesToGo

func JSONBytesToGo(filename, pkgname, rootname string, b *[]byte) error

JSONBytesToGo convert json bytes to struct, then the struct will be writed to ./{pkg}/{filename}.go

func XMLBytesToGo

func XMLBytesToGo(filename, pkgname string, b *[]byte) error

XMLBytesToGo convert xml bytes to struct, then the struct will be writed to ./{pkg}/{filename}.go

Types

type XJ

type XJ struct {
	// xml or json file
	Filepath string
	// the pkg name for struct
	Pkgname string
	// the root name for json bytes
	Rootname string
}

XJ define xj2go struct

func New

func New(filepath, pkgname, rootname string) *XJ

New return a xj2go instance

func (*XJ) JSONToGo

func (xj *XJ) JSONToGo() error

JSONToGo convert json to go struct, then write this struct to a go file

func (*XJ) XMLToGo

func (xj *XJ) XMLToGo() error

XMLToGo convert xml to go struct, then write this struct to a go file

Directories

Path Synopsis
cmd
xj

Jump to

Keyboard shortcuts

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