Golang helper to copy/convert fields between two different structures
Go to file
2020-01-15 00:00:02 +01:00
.github/workflows Create go.yml 2020-01-15 00:00:02 +01:00
examples Initial commit with main funcs and tests 2020-01-14 23:57:56 +01:00
.gitignore Initial commit with main funcs and tests 2020-01-14 23:57:56 +01:00
.travis.yml Initial commit with main funcs and tests 2020-01-14 23:57:56 +01:00
common.go Initial commit with main funcs and tests 2020-01-14 23:57:56 +01:00
fields_test.go Initial commit with main funcs and tests 2020-01-14 23:57:56 +01:00
fields.go Initial commit with main funcs and tests 2020-01-14 23:57:56 +01:00
go.mod Initial commit with main funcs and tests 2020-01-14 23:57:56 +01:00
go.sum Initial commit with main funcs and tests 2020-01-14 23:57:56 +01:00
LICENSE Initial commit with main funcs and tests 2020-01-14 23:57:56 +01:00
README.md Initial commit with main funcs and tests 2020-01-14 23:57:56 +01:00

Go Report Card Actions Status Build Status codecov

golang convert fields lib

Idea

Main idea was to build a lib/helper to simplify copying same-name fields from one struct object to another one.

Examples

Example 1: most common usage, with sub-cases

import (
	"log"
	"encoding/json"

	"github.com/go-convert/fields"
)

type FromStruct struct {
	ID     string `conv:"new,update"`
	Field1 string `conv:"new,update"`
	Field2 string `conv:"update"`
}

type ToStruct struct {
	ID     string
	Field1 string
	Field2 string
	Field3 string
}

func main() {
	fromDefault := FromStruct{
		"1", "field 1 value", "field 2 value",
	}
	toDefault := ToStruct{
		"old val 1", "old field 1 value", "old field 2 value", "old field 3 value",
	}

	log.Print("example 1.1 - new")
	from := fromDefault
	to := toDefault

	fields.Сonvert(from, "new", &to)
	fromB, _ := json.Marshal(from)
	toB, _ := json.Marshal(to)

	log.Printf("from %s => to %s", string(fromB), string(toB))

	log.Print("example 1.2 - update")
	from = fromDefault
	to = toDefault

	fields.Сonvert(from, "update", &to)
	fromB, _ = json.Marshal(from)
	toB, _ = json.Marshal(to)

	log.Printf("from %s => to %s", string(fromB), string(toB))
}

After running go run examples/example-1/main.go

Result will be something like below:

2020/01/14 21:46:32 example 1.1 - new
2020/01/14 21:46:32 from {"ID":"1","Field1":"field 1 value","Field2":"field 2 value"} => to {"ID":"1","Field1":"field 1 value","Field2":"old field 2 value","Field3":"old field 3 value"}
2020/01/14 21:46:32 example 1.2 - update
2020/01/14 21:46:32 from {"ID":"1","Field1":"field 1 value","Field2":"field 2 value"} => to {"ID":"1","Field1":"field 1 value","Field2":"field 2 value","Field3":"old field 3 value"}

Example 2: custom config setup usage, with sub-cases

  • New name of tag: copy
  • New tag value for omitting: ignore
  • New tag value for omitting empty field: noempty
import (
	"log"
	"encoding/json"

	"github.com/go-convert/fields"
)

type FromStructCustom struct {
	ID     string `copy:"new,update"`
	Field1 string `copy:"new,update"`
	Field2 string `copy:"update"`
	Field3 string `copy:"category-1,ignore"`
	Field4 string `copy:"category-1,noempty"`
}

type ToStruct struct {
	ID     string
	Field1 string
	Field2 string
	Field3 string
	Field4 string
}

func main() {
	fields.SetTagName("copy")
	fields.SetOmitTagName("ignore")
	fields.SetOmitEmptyTagName("noempty")

	log.Print("example 2.1 - new")
	from := FromStructCustom{
		"1", "field 1 value", "field 2 value", "omitting", "not empty",
	}
	to := ToStruct{
		"old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "old field 4 value",
	}

	fields.Сonvert(from, "new", &to)
	fromB, _ := json.Marshal(from)
	toB, _ := json.Marshal(to)

	log.Printf("from %s => to %s", string(fromB), string(toB))

	log.Print("example 2.2.1 - category-1")
	from = FromStructCustom{
		"1", "field 1 value", "field 2 value", "omitting", "not empty",
	}
	to = ToStruct{
		"old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "old field 4 value",
	}

	fields.Сonvert(from, "category-1", &to)
	fromB, _ = json.Marshal(from)
	toB, _ = json.Marshal(to)

	log.Printf("from %s => to %s", string(fromB), string(toB))

	log.Print("example 2.2.2 - category-1")
	from = FromStructCustom{
		"1", "field 1 value", "field 2 value", "omitting", "",
	}
	to = ToStruct{
		"old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "old field 4 value",
	}

	fields.Сonvert(from, "category-1", &to)
	fromB, _ = json.Marshal(from)
	toB, _ = json.Marshal(to)

	log.Printf("from %s => to %s", string(fromB), string(toB))
}

After running go run examples/example-1/main.go

Result will be something like below:

2020/01/14 23:13:26 example 2.1 - new
2020/01/14 23:13:26 from {"ID":"1","Field1":"field 1 value","Field2":"field 2 value","Field3":"omitting","Field4":"not empty"} => to {"ID":"1","Field1":"field 1 value","Field2":"old field 2 value","Field3":"old field 3 value","Field4":"old field 4 value"}
2020/01/14 23:13:26 example 2.2.1 - category-1
2020/01/14 23:13:26 from {"ID":"1","Field1":"field 1 value","Field2":"field 2 value","Field3":"omitting","Field4":"not empty"} => to {"ID":"old val 1","Field1":"old field 1 value","Field2":"old field 2 value","Field3":"old field 3 value","Field4":"not empty"}
2020/01/14 23:13:26 example 2.2.2 - category-1
2020/01/14 23:13:26 from {"ID":"1","Field1":"field 1 value","Field2":"field 2 value","Field3":"omitting","Field4":""} => to {"ID":"old val 1","Field1":"old field 1 value","Field2":"old field 2 value","Field3":"old field 3 value","Field4":"old field 4 value"}

License

Copyright (c) 2020 Alex aka mailoman alex@webz.asia

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.