-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from dumim/develop
Fixed panics on unexported fields
- Loading branch information
Showing
4 changed files
with
180 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Use the latest 2.1 version of CircleCI pipeline process engine. See: https://circleci.com/docs/2.0/configuration-reference | ||
version: 2.1 | ||
jobs: | ||
build: | ||
working_directory: ~/repo | ||
docker: | ||
- image: circleci/golang:1.15.8 | ||
steps: | ||
- checkout | ||
- restore_cache: | ||
keys: | ||
- go-mod-v4-{{ checksum "go.sum" }} | ||
- run: | ||
name: Install Dependencies | ||
command: | | ||
go mod download | ||
go get github.com/mattn/goveralls | ||
- save_cache: | ||
key: go-mod-v4-{{ checksum "go.sum" }} | ||
paths: | ||
- "/go/pkg/mod" | ||
- run: | ||
name: Run tests | ||
command: | | ||
mkdir -p /tmp/test-reports | ||
gotestsum --junitfile /tmp/test-reports/unit-tests.xml | ||
go test -v -cover -race -coverprofile=/home/ubuntu/coverage.out | ||
/home/ubuntu/.go_workspace/bin/goveralls -coverprofile=/home/ubuntu/coverage.out -service=circle-ci -repotoken=$COVERALLS_TOKEN | ||
- store_test_results: | ||
path: /tmp/test-reports |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,132 @@ | ||
[![Documentation](https://godoc.org/github.com/dumim/tagconv?status.svg)](http://godoc.org/github.com/dumim/tagconv) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/dumim/tagconv)](https://goreportcard.com/report/github.com/dumim/tagconv) | ||
[![MIT License](https://img.shields.io/apm/l/atomic-design-ui.svg?)](https://github.com/tterb/atomic-design-ui/blob/master/LICENSEs) | ||
|
||
# TagConv | ||
Convert any Go Struct to a Map based on custom struct tags with dot notation | ||
Convert any Go Struct to a Map based on custom struct tags with dot notation. | ||
|
||
## Background | ||
This package tries to simplify certain use-cases where a struct needs to be mapped manually to a different struct, which holds the same data but is organised differently. | ||
(eg: mapping data from the db to a presentable API output) | ||
|
||
This package allows you to use custom struct tags (which could be any string) to define the mapping. | ||
This mapping follows the dot-notation convention. Example: | ||
```go | ||
Hello string `mytag:"hello.world"` | ||
``` | ||
The above will result in a map with the JSON equivalent of: | ||
```json | ||
{ | ||
"hello": { | ||
"world": "hello world" | ||
} | ||
} | ||
``` | ||
## Usage/Examples | ||
|
||
Import the package | ||
```go | ||
import "github.com/dumim/tagconv" | ||
``` | ||
|
||
Given a deeply-nested complex struct with custom tags like below: | ||
```go | ||
type Obj struct { | ||
Name string `custom:"name"` | ||
Text string `custom:"text"` | ||
World string `custom:"data.world"` // dot notation inside nested struct | ||
} | ||
type ObjTwo struct { | ||
Hello string `custom:"hello"` | ||
Text string `custom:"data.text"` | ||
} | ||
type ObjThree struct { | ||
Name string `custom:"name"` | ||
Value int `custom:"value"` | ||
} | ||
type Example struct { | ||
Name string `custom:"name"` | ||
Email string `custom:"email"` | ||
Obj Obj `custom:"object"` | ||
ObjTwo ObjTwo // no tag | ||
ObjThree ObjTwo `custom:"-"` // explicitly ignored | ||
Id int `custom:"id"` | ||
Call int `custom:"data.call"` // top-level dot notation | ||
ArrayObj []ObjThree `custom:"list"` | ||
} | ||
``` | ||
The `ToMap` function can be used to convert this into a JSON/Map based on the values defined in the given custom tag like so. | ||
```go | ||
obj := Example{ | ||
Name: "2", | ||
Email: "3", | ||
Obj: Obj{ | ||
Name: "4", | ||
Text: "5", | ||
World: "6", | ||
}, | ||
ObjTwo: ObjTwo{ | ||
Hello: "1", | ||
Text: "2", | ||
}, | ||
Id: 01, | ||
Call: 02, | ||
ArrayObj: []ObjThree{ | ||
{"hi", 1}, | ||
{"world", 2}, | ||
}, | ||
} | ||
|
||
// get the map from custom tags | ||
tagName = "custom" | ||
myMap, err := ToMap(obj, tagName) | ||
if err != nil { | ||
panic() | ||
} | ||
|
||
myMapJSON, err := json.MarshalIndent(myMap, "", " ") | ||
if err != nil { | ||
panic() | ||
} | ||
|
||
fmt.Print(myMapJSON) | ||
|
||
``` | ||
This will produce a result similar to: | ||
```json | ||
{ | ||
"name": "2", | ||
"email": "3", | ||
"object": { | ||
"name": "4", | ||
"text": "5", | ||
"data": { | ||
"world": "6" | ||
} | ||
}, | ||
"hello": "1", | ||
"data": { | ||
"text": "2", | ||
"call": 2 | ||
}, | ||
"id": 1, | ||
"list": [ | ||
{ | ||
"name": "hi", | ||
"value": 1 | ||
}, | ||
{ | ||
"name": "world", | ||
"value": 2 | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## Acknowledgements | ||
|
||
- [Helpful Stackoverflow answer](https://stackoverflow.com/a/7794127/10340220) | ||
- [Mergo](https://github.com/imdario/mergo) | ||
## Contributing | ||
|
||
TODO: installation, examples, how-to, tests | ||
Contributions are always welcome! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters