fields/examples/example-3/main.go

76 lines
1.8 KiB
Go
Raw Normal View History

/*
* 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.
*
* @author Alex aka mailoman <alex@webz.asia>
* @copyright Copyright (c) 2020 Alex aka mailoman <alex@webz.asia>
* @since 26.01.2020
*
*/
package main
import (
"encoding/json"
"log"
"github.com/go-convert/fields"
)
type FromStructBase struct {
ID string `conv:"new,update"`
Field1 string `conv:"new,update"`
}
type fromStruct struct {
FromStructBase `conv:"new,update"`
Field2 string `conv:"update"`
}
type toStruct struct {
ID string
Field1 string
Field2 string
Field3 string
}
func main() {
fromDefault := fromStruct{
FromStructBase{"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 3.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 3.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))
}