structs

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

structs

structs provides utility features related of the struct.

Features

  • propagation struct fields
  • output diff struct fields
propagation struct fields

struct fields in the struct can be copied from variable A to B.

Usage
package structs_test

import (
	"fmt"

	"github.com/takashabe/structs"
)

func ExamplePropagateValues() {
	type User struct {
		ID   int
		Name string
		Age  int
	}

	a := &User{ID: 1, Name: "foo", Age: 1}
	b := &User{ID: 2, Name: "bar", Age: 2}
	got, _ := structs.PropagateValues(
		a,
		b,
		structs.PropagateWithIgnoreFields("ID", "Name"),
		structs.PropagateWithValue(1),
	)
	fmt.Printf("%+v", got)

	// Output:
	// &{ID:2 Name:bar Age:1}
}
output diff struct fields

provide output of field names with different values

Usage
func ExampleDiffFields() {
	type User struct {
		ID     int         `json:"id"`
		Name   string      `json:"name"`
		Age    int         `json:"age"`
		Gender null.String `json:"gender"`
	}

	a := &User{ID: 1, Name: "foo", Age: 1, Gender: null.StringFrom("Male")}
	b := &User{ID: 2, Name: "bar", Age: 1}
	got, _ := structs.DiffFields(
		a,
		b,
	)
	fmt.Printf("%+v", got)

	// Output:
	// [id name gender]
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DiffTagName = "json"

Functions

func DiffFields added in v0.0.4

func DiffFields[T, T2 any](src T, dst T2, opts ...Option) ([]string, error)

DiffFields returns different field names

Example
type User struct {
	ID     int         `json:"id"`
	Name   string      `json:"name"`
	Age    int         `json:"age"`
	Gender null.String `json:"gender"`
}

a := &User{ID: 1, Name: "foo", Age: 1, Gender: null.StringFrom("Male")}
b := &User{ID: 2, Name: "bar", Age: 1}
got, _ := structs.DiffFields(
	a,
	b,
)
fmt.Printf("%+v", got)
Output:

[id name gender]

func PropagateValues

func PropagateValues[T, T2 any](src T, dst T2, opts ...Option) (T2, error)

PropagateValues copy from `src` field values to `dst`.

Example
package main

import (
	"fmt"

	"github.com/takashabe/structs"
)

func main() {
	type User struct {
		ID   int
		Name string
		Age  int
	}

	a := &User{ID: 1, Name: "foo", Age: 1}
	b := &User{ID: 2, Name: "bar", Age: 2}
	got, _ := structs.PropagateValues(
		a,
		b,
		structs.WithIgnoreFields("ID", "Name"),
		structs.WithValue(1),
	)
	fmt.Printf("%+v", got)

}
Output:

&{ID:2 Name:bar Age:1}

Types

type Option added in v0.0.4

type Option func(srcValue, dstValue reflect.Value, srcField reflect.StructField) bool

Option represents whether to target field.

func WithDefaultValue added in v0.0.4

func WithDefaultValue() Option

WithDefaultValue valid when the field in dst is a type-specific default value.

func WithIgnoreFields added in v0.0.4

func WithIgnoreFields(names ...string) Option

func WithIgnoreSourceDefaultValue added in v0.0.4

func WithIgnoreSourceDefaultValue() Option

WithIgnoreSourceDefaultValue not valid if the src field is a type-specific default value.

func WithTargetFields added in v0.0.4

func WithTargetFields(names ...string) Option

func WithValue added in v0.0.4

func WithValue(v any) Option

Jump to

Keyboard shortcuts

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