Giter VIP home page Giter VIP logo

go-struct-value's Introduction

go-struct-value

library for get all primitive value of struct. support for use with pointer property. but cannot use with nested struct

How to install

 go get github.com/satitza/go-struct-value

How to use

create your struct and global variable for store custom name of property if your want to custom name

var customFieldName = map[string]string{
 "test_string_ptr": "custom_test_string_ptr"
}

var addCustomFields = map[string]any{
 "add_custom_field": "custom_value"
}

var convertDateToEpoch = []string{"test_epoch_time"} // support date format 2006-01-02 15:04:05

type CustomStruct struct {
  TestStringPtr *string                 `json:"test_string_ptr"`
  TestIntPtr    *int                    `json:"test_int_ptr"`
  TestBoolPtr   *bool                   `json:"test_bool_ptr"`
  TestString    string                  `json:"test_string"`
  TestInt       int                     `json:"test_int"`
  TestBool      bool                    `json:"test_bool"`
  TestEpochTime string                  `json:"test_epoch_time"`
  TestJson      go_struct_value.MapJson `json:"test_json"`
} 

create receiver function for call function in library and pass parameter

func (req CustomStruct) GetAllColumnsNameString() ([]string, error) {
   return go_struct_value.GetAllColumnsName(req, customFieldName, addCustomFields)
}

func (req CustomStruct) GetFieldInsertData() ([]string, []any, error) {
   return go_struct_value.GetSqlAndDataForInsert(req, customFieldName, addCustomFields, convertDateToEpoch)
}

func (req CustomStruct) GetFieldUpdateMap() (map[string]any, error) {
   return go_struct_value.GetFieldValueMap(req, customFieldName, convertDateToEpoch)
}

func (req CustomStruct) GetAllFieldValueMap() (map[string]any, error) {
   return go_struct_value.GetAllFieldValueMap(req, customFieldName, convertDateToEpoch)
}

- GetAllColumnsNameString return all property name array and convert to snake case

- GetFieldInsertData return all property name array(string snake case) and array value of all property

- GetFieldUpdateMap return map[string]any the key is property name (string snake case) value is a value of property. but get kay and value if property has value only ( not nil or empty string)

- GetAllFieldValueMap return map[string]any the key is property name (string snake case) value is a value of property include nil or empty string

Example

var testStringPtr = "testStringPtr"
  var testIntPtr = 1234
  var testBoolPtr = true

  var customStruct = model.CustomStruct{
  	TestStringPtr: &testStringPtr,
  	TestIntPtr:    &testIntPtr,
  	TestBoolPtr:   &testBoolPtr,
  	TestString:    "TestString",
  	TestInt:       4321,
  	TestBool:      false,
  	TestEpochTime: "2024-02-04 13:53:05",
  	TestJson: map[string]interface{}{
  		"col1": "col1",
  	},
  }

  var mapFieldValues, err = customStruct.GetAllFieldValueMap()
  if err != nil {
  	fmt.Println(err)
  }

  for key, value := range mapFieldValues {
  	fmt.Printf("Key: %s, Value: %v\n", key, value)
  }

Output

  Key: test_json, Value: [123 34 99 111 108 49 34 58 34 99 111 108 49 34 125]
  Key: test_string_ptr, Value: testStringPtr
  Key: test_int_ptr, Value: 1234
  Key: test_bool_ptr, Value: true
  Key: test_string, Value: TestString
  Key: test_int, Value: 4321
  Key: test_bool, Value: false
  Key: test_epoch_time, Value: 1707029585

go-struct-value's People

Contributors

satitza avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.