fields

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

README

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"

	"git.webz.asia/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"

	"git.webz.asia/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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertTagName

func ConvertTagName() string

ConvertTagName returns special tag name

func OmitEmptyTagName

func OmitEmptyTagName() string

OmitEmptyTagName returns omitting empty field tag name

func OmitTagName

func OmitTagName() string

OmitTagName returns omitting tag name

func SetDefaults

func SetDefaults()

SetDefaults resets all configured tag names to defaults

func SetOmitEmptyTagName

func SetOmitEmptyTagName(value string)

SetOmitEmptyTagName sets omitting empty field tag name

func SetOmitTagName

func SetOmitTagName(value string)

SetOmitTagName sets omitting tag name

func SetTagName

func SetTagName(value string)

SetTagName sets special tag name

func Сonvert

func Сonvert(from interface{}, kind string, to interface{})

Convert copies field values from one struct to another based on tags setup

Types

This section is empty.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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