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

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)
}
Owner
Afeyer
Changing the world with code
Afeyer
Similar Resources

Convert Go values to their AST

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

Dec 21, 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.

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

Aug 15, 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,

Dec 12, 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-

Dec 22, 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

Jun 28, 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.

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.

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

Aug 28, 2022
Comments
  • 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

Dasel - Select, put and delete data from JSON, TOML, YAML, XML and CSV files with a single tool.
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.

Jan 1, 2023
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

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

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

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

Jan 8, 2023
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

Aug 4, 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

Dec 16, 2022
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

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

Dec 25, 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

Dec 20, 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

Jan 5, 2023