fields/examples/example-1/main.go

72 lines
1.7 KiB
Go
Raw Permalink 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 14.01.2020
*
*/
package main
import (
"encoding/json"
"log"
"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))
}