github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/g/struct_example_test.go (about)

     1  package g_test
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/angenalZZZ/gofunc/g"
     6  	"time"
     7  )
     8  
     9  func ExampleNew() {
    10  	type Server struct {
    11  		Name    string
    12  		ID      int32
    13  		Enabled bool
    14  	}
    15  
    16  	server := &Server{
    17  		Name:    "Arslan",
    18  		ID:      123456,
    19  		Enabled: true,
    20  	}
    21  
    22  	s := g.NewStruct(server)
    23  
    24  	fmt.Printf("Name        : %v\n", s.Name())
    25  	fmt.Printf("Values      : %v\n", s.Values())
    26  	fmt.Printf("Value of ID : %v\n", s.Field("ID").Value())
    27  	// Output:
    28  	// Name        : Server
    29  	// Values      : [Arslan 123456 true]
    30  	// Value of ID : 123456
    31  
    32  }
    33  
    34  func ExampleMap() {
    35  	type Server struct {
    36  		Name    string
    37  		ID      int32
    38  		Enabled bool
    39  	}
    40  
    41  	s := &Server{
    42  		Name:    "Arslan",
    43  		ID:      123456,
    44  		Enabled: true,
    45  	}
    46  
    47  	m := g.Maps(s)
    48  
    49  	fmt.Printf("%#v\n", m["Name"])
    50  	fmt.Printf("%#v\n", m["ID"])
    51  	fmt.Printf("%#v\n", m["Enabled"])
    52  	// Output:
    53  	// "Arslan"
    54  	// 123456
    55  	// true
    56  
    57  }
    58  
    59  func ExampleMap_tags() {
    60  	// Custom tags can change the map keys instead of using the fields name
    61  	type Server struct {
    62  		Name    string `struct:"server_name"`
    63  		ID      int32  `struct:"server_id"`
    64  		Enabled bool   `struct:"enabled"`
    65  	}
    66  
    67  	s := &Server{
    68  		Name: "Zeynep",
    69  		ID:   789012,
    70  	}
    71  
    72  	m := g.Maps(s)
    73  
    74  	// access them by the custom tags defined above
    75  	fmt.Printf("%#v\n", m["server_name"])
    76  	fmt.Printf("%#v\n", m["server_id"])
    77  	fmt.Printf("%#v\n", m["enabled"])
    78  	// Output:
    79  	// "Zeynep"
    80  	// 789012
    81  	// false
    82  
    83  }
    84  
    85  func ExampleMap_omitNested() {
    86  	// By default field with struct types are processed too. We can stop
    87  	// processing them via "omitnested" tag option.
    88  	type Server struct {
    89  		Name string    `struct:"server_name"`
    90  		ID   int32     `struct:"server_id"`
    91  		Time time.Time `struct:"time,omitnested"` // do not convert to map[string]interface{}
    92  	}
    93  
    94  	//const shortForm = "2006-Jan-02"
    95  	t, _ := time.Parse("2006-Jan-02", "2013-Feb-03")
    96  
    97  	s := &Server{
    98  		Name: "Zeynep",
    99  		ID:   789012,
   100  		Time: t,
   101  	}
   102  
   103  	m := g.Maps(s)
   104  
   105  	// access them by the custom tags defined above
   106  	fmt.Printf("%v\n", m["server_name"])
   107  	fmt.Printf("%v\n", m["server_id"])
   108  	fmt.Printf("%v\n", m["time"].(time.Time))
   109  	// Output:
   110  	// Zeynep
   111  	// 789012
   112  	// 2013-02-03 00:00:00 +0000 UTC
   113  }
   114  
   115  func ExampleMap_omitEmpty() {
   116  	// By default field with struct types of zero values are processed too. We
   117  	// can stop processing them via "omitempty" tag option.
   118  	type Server struct {
   119  		Name     string `struct:",omitempty"`
   120  		ID       int32  `struct:"server_id,omitempty"`
   121  		Location string
   122  	}
   123  
   124  	// Only add location
   125  	s := &Server{
   126  		Location: "Tokyo",
   127  	}
   128  
   129  	m := g.Maps(s)
   130  
   131  	// map contains only the Location field
   132  	fmt.Printf("%v\n", m)
   133  	// Output:
   134  	// map[Location:Tokyo]
   135  }
   136  
   137  func ExampleValues() {
   138  	type Server struct {
   139  		Name    string
   140  		ID      int32
   141  		Enabled bool
   142  	}
   143  
   144  	s := &Server{
   145  		Name:    "Fatih",
   146  		ID:      135790,
   147  		Enabled: false,
   148  	}
   149  
   150  	m := g.Values(s)
   151  
   152  	fmt.Printf("Values: %+v\n", m)
   153  	// Output:
   154  	// Values: [Fatih 135790 false]
   155  }
   156  
   157  func ExampleValues_omitEmpty() {
   158  	// By default field with struct types of zero values are processed too. We
   159  	// can stop processing them via "omitempty" tag option.
   160  	type Server struct {
   161  		Name     string `struct:",omitempty"`
   162  		ID       int32  `struct:"server_id,omitempty"`
   163  		Location string
   164  	}
   165  
   166  	// Only add location
   167  	s := &Server{
   168  		Location: "Ankara",
   169  	}
   170  
   171  	m := g.Values(s)
   172  
   173  	// values contains only the Location field
   174  	fmt.Printf("Values: %+v\n", m)
   175  	// Output:
   176  	// Values: [Ankara]
   177  }
   178  
   179  func ExampleValues_tags() {
   180  	type Location struct {
   181  		City    string
   182  		Country string
   183  	}
   184  
   185  	type Server struct {
   186  		Name     string
   187  		ID       int32
   188  		Enabled  bool
   189  		Location Location `struct:"-"` // values from location are not included anymore
   190  	}
   191  
   192  	s := &Server{
   193  		Name:     "Fatih",
   194  		ID:       135790,
   195  		Enabled:  false,
   196  		Location: Location{City: "Ankara", Country: "Turkey"},
   197  	}
   198  
   199  	// Let get all values from the struct s. Note that we don't include values
   200  	// from the Location field
   201  	m := g.Values(s)
   202  
   203  	fmt.Printf("Values: %+v\n", m)
   204  	// Output:
   205  	// Values: [Fatih 135790 false]
   206  }
   207  
   208  func ExampleFields() {
   209  	type Access struct {
   210  		Name         string
   211  		LastAccessed time.Time
   212  		Number       int
   213  	}
   214  
   215  	s := &Access{
   216  		Name:         "Fatih",
   217  		LastAccessed: time.Now(),
   218  		Number:       1234567,
   219  	}
   220  
   221  	fields := g.Fields(s)
   222  
   223  	for i, field := range fields {
   224  		fmt.Printf("[%d] %+v\n", i, field.Name())
   225  	}
   226  
   227  	// Output:
   228  	// [0] Name
   229  	// [1] LastAccessed
   230  	// [2] Number
   231  }
   232  
   233  func ExampleFields_nested() {
   234  	type Person struct {
   235  		Name   string
   236  		Number int
   237  	}
   238  
   239  	type Access struct {
   240  		Person        Person
   241  		HasPermission bool
   242  		LastAccessed  time.Time
   243  	}
   244  
   245  	s := &Access{
   246  		Person:        Person{Name: "fatih", Number: 1234567},
   247  		LastAccessed:  time.Now(),
   248  		HasPermission: true,
   249  	}
   250  
   251  	// Let's get all fields from the struct s.
   252  	fields := g.Fields(s)
   253  
   254  	for _, field := range fields {
   255  		if field.Name() == "Person" {
   256  			fmt.Printf("Access.Person.Name: %+v\n", field.Field("Name").Value())
   257  		}
   258  	}
   259  
   260  	// Output:
   261  	// Access.Person.Name: fatih
   262  }
   263  
   264  func ExampleField() {
   265  	type Person struct {
   266  		Name   string
   267  		Number int
   268  	}
   269  
   270  	type Access struct {
   271  		Person        Person
   272  		HasPermission bool
   273  		LastAccessed  time.Time
   274  	}
   275  
   276  	access := &Access{
   277  		Person:        Person{Name: "fatih", Number: 1234567},
   278  		LastAccessed:  time.Now(),
   279  		HasPermission: true,
   280  	}
   281  
   282  	// Create a new Struct type
   283  	s := g.NewStruct(access)
   284  
   285  	// Get the Field type for "Person" field
   286  	p := s.Field("Person")
   287  
   288  	// Get the underlying "Name field" and print the value of it
   289  	name := p.Field("Name")
   290  
   291  	fmt.Printf("Value of Person.Access.Name: %+v\n", name.Value())
   292  
   293  	// Output:
   294  	// Value of Person.Access.Name: fatih
   295  
   296  }
   297  
   298  func ExampleIsZero() {
   299  	type Server struct {
   300  		Name    string
   301  		ID      int32
   302  		Enabled bool
   303  	}
   304  
   305  	// Nothing is initialized
   306  	a := &Server{}
   307  	isZeroA := g.IsZero(a)
   308  
   309  	// Name and Enabled is initialized, but not ID
   310  	b := &Server{
   311  		Name:    "Golang",
   312  		Enabled: true,
   313  	}
   314  	isZeroB := g.IsZero(b)
   315  
   316  	fmt.Printf("%#v\n", isZeroA)
   317  	fmt.Printf("%#v\n", isZeroB)
   318  	// Output:
   319  	// true
   320  	// false
   321  }
   322  
   323  func ExampleHasZero() {
   324  	// Let's define an Access struct. Note that the "Enabled" field is not
   325  	// going to be checked because we added the "struct" tag to the field.
   326  	type Access struct {
   327  		Name         string
   328  		LastAccessed time.Time
   329  		Number       int
   330  		Enabled      bool `struct:"-"`
   331  	}
   332  
   333  	// Name and Number is not initialized.
   334  	a := &Access{
   335  		LastAccessed: time.Now(),
   336  	}
   337  	hasZeroA := g.HasZero(a)
   338  
   339  	// Name and Number is initialized.
   340  	b := &Access{
   341  		Name:         "Fatih",
   342  		LastAccessed: time.Now(),
   343  		Number:       12345,
   344  	}
   345  	hasZeroB := g.HasZero(b)
   346  
   347  	fmt.Printf("%#v\n", hasZeroA)
   348  	fmt.Printf("%#v\n", hasZeroB)
   349  	// Output:
   350  	// true
   351  	// false
   352  }