Convert arbitrary formats to Go Struct (including json, toml, yaml, etc.)

Overview

go2struct

Convert arbitrary formats to Go Struct (including json, toml, yaml, etc.)

Installation

Run the following command under your project:

go get -u github.com/NICEXAI/go2struct

Feature

  • Map to struct
  • JSON to struct
  • YAML to struct
  • TOML to struct

Example

package main

import (
	"encoding/json"
	"fmt"
	"github.com/NICEXAI/go2struct"
)

type Message struct {
	Code    int  `json:"code"`
	Success bool `json:"success"`
	Msg     struct {
		Content string
	} `json:"msg"`
}

func main() {
	var options map[string]interface{}

	message := Message{
		Code: 200,
		Msg:  struct{ Content string }{Content: "Hello"},
	}

	bData, err := json.Marshal(message)
	if err != nil {
		fmt.Printf("json marshal failed, error: %v", err)
		return
	}
	_ = json.Unmarshal(bData, &options)

	//map to struct
	structTxt := go2struct.Map2Struct(options)

	fmt.Printf("map to struct success, result: \n%s", structTxt)

	// json to struct
	jsonTemp := `
	{
	 "code": 200,
	 "success": true,
	 "msg": {
		"content": "Hello"
	 }
	}
	`

	res, err := go2struct.JSON2Struct([]byte(jsonTemp))
	if err != nil {
		fmt.Printf("json to struct failed, error: \n%v", err)
		return
	}
	fmt.Printf("json to struct success, result: \n%s", res)

	// yaml to struct
	temp := `
code: 200
success: true
msg:
 content: Hello
`

	res, err = go2struct.YAML2Struct([]byte(temp))
	if err != nil {
		fmt.Printf("yaml to struct failed, error: \n%v", err)
		return
	}
	fmt.Printf("yaml to struct success, result: \n%s", res)
}
Issues
  • yaml to struct failed

    yaml to struct failed

    Not able to convert my yaml to gostruct Error faced: yaml to struct failed, error: yaml: unmarshal errors: line 2: cannot unmarshal !!seq into map[string]interface {} yaml used:

    - !variable
      id: db-password
      kind: password
    

    yamltogolang struct yamltogolang-error

    bug good first issue 
    opened by GeethaSelvam 1
Releases(v0.1.2)
Owner
Afeyer
Changing the world with code
Afeyer
Dasel - Select, put and delete data from JSON, TOML, YAML, XML and CSV files with a single tool.

Select, put and delete data from JSON, TOML, YAML, XML and CSV files with a single tool. Supports conversion between formats and can be used as a Go package.

Tom Wright 3.3k Jun 24, 2022
Convert json string to Golang struct

json-to-go-cli Convert json string to Golang struct How to install git clone https://github.com/tiancheng92/json-to-go-cli.git cd json-to-go-cli go bu

TianCheng 7 May 10, 2022
Generic types that are missing from Go, including sets, trees, sorted lists, etc.

go-typ Generic types that are missing from Go, including sets, trees, sorted lists, etc. All code is implemented with 0 dependencies and in pure Go co

null 10 May 11, 2022
An interesting go struct tag expression syntax for field validation, etc.

An interesting go struct tag expression syntax for field validation, etc.

Bytedance Inc. 1.2k Jun 25, 2022
A skip list of arbitrary elements that can be filtered using roaring bitmaps stored in an LRU cache

Skipfilter This package provides a data structure that combines a skiplist with a roaring bitmap cache. This library was created to efficiently filter

Kevin Burns 20 Apr 7, 2022
peanut is a Go package to write tagged data structs to disk in a variety of formats.

peanut peanut is a Go package to write tagged data structs to disk in a variety of formats. Its primary purpose is to provide a single consistent inte

Jim Smart 3 Apr 21, 2021
Clone a directory (including permissions) into S3 for File Gateway usage

s3-tree-clone Clone a filesystem tree to S3 (including metadata), skipping over files that are already synced, in a manner compatible with AWS File Ga

David Cuthbert 1 Dec 2, 2021
Juniper is an extension to the Go standard library using generics, including containers, iterators, and streams.

Juniper Juniper is a library of extensions to the Go standard library using generics, including containers, iterators, and streams. container/tree con

Braden Walker 476 Jun 29, 2022
Golang string comparison and edit distance algorithms library, featuring : Levenshtein, LCS, Hamming, Damerau levenshtein (OSA and Adjacent transpositions algorithms), Jaro-Winkler, Cosine, etc...

Go-edlib : Edit distance and string comparison library Golang string comparison and edit distance algorithms library featuring : Levenshtein, LCS, Ham

Hugo Bollon 329 Jun 21, 2022
Converts PDF, DOC, DOCX, XML, HTML, RTF, etc to plain text

docconv A Go wrapper library to convert PDF, DOC, DOCX, XML, HTML, RTF, ODT, Pages documents and images (see optional dependencies below) to plain tex

Search.io 959 Jun 23, 2022
A faster method to get elements from an interface (Struct or Slice type) for Go.

A faster method to get elements from an interface (Struct or Slice type) for Go.

karminski-牙医 35 May 13, 2022
GoStruct2Table - format your struct like a table.

GoStruct2Table format your struct like a table. Installing $ go get -u -v github.com/runningzyp/GoStruct2Table Simple Example import parser "github.c

YunpengZhan 9 Jun 14, 2022
Convert Go values to their AST

valast - convert Go values to their AST Valast converts Go values at runtime into their go/ast equivalent,

Hexops 257 Jun 10, 2022
Nullable Go types that can be marshalled/unmarshalled to/from JSON.

Nullable Go types Description This package provides nullable Go types for bool, float64, int64, int32, string and time.Time replacing sql.NullString,

Emvi 25 Apr 20, 2022
Decode / encode XML to/from map[string]interface{} (or JSON); extract values with dot-notation paths and wildcards. Replaces x2j and j2x packages.

mxj - to/from maps, XML and JSON Decode/encode XML to/from map[string]interface{} (or JSON) values, and extract/modify values from maps by key or key-

Charles Banning 521 Jun 28, 2022
Generates data structure definitions from JSON files for any kind of programming language

Overview Archivist generates data structure definitions from JSON files for any kind of programming language. It also provides a library for golang to

Kingsgroup 44 May 4, 2022
estruct traverses javascript projects and maps all the dependencies and relationships to a JSON. the output can be used to build network visualizations of the project and document the architecture.

EStruct traverses javascript projects and maps all the dependencies and relationships to a JSON. The output can be used to build network visualizations of the project and document the architecture.

Ray Luxembourg 11 Jan 27, 2022
Custom generic HTTP handler providing automatic JSON decoding/encoding of HTTP request/response to your concrete types

gap Custom generic HTTP handler providing automatic JSON decoding/encoding of HTTP request/response to your concrete types. gap.Wrap allows to use the

Ectobit 0 Jan 5, 2022