github.com/RevenueMonster/sqlike@v1.0.6/examples/types.go (about)

     1  package examples
     2  
     3  import (
     4  	"encoding/json"
     5  	"sort"
     6  	"time"
     7  
     8  	"cloud.google.com/go/civil"
     9  	"github.com/RevenueMonster/sqlike/types"
    10  	"github.com/brianvoe/gofakeit"
    11  	"golang.org/x/text/currency"
    12  	"golang.org/x/text/language"
    13  
    14  	"github.com/google/uuid"
    15  )
    16  
    17  type indexStruct struct {
    18  	Unique string `sqlike:",unique_index"`
    19  	ID     string `sqlike:""`
    20  }
    21  
    22  // Model :
    23  type Model struct {
    24  	CreatedAt time.Time
    25  	UpdatedAt time.Time
    26  }
    27  
    28  type normalStruct struct {
    29  	ID            uuid.UUID `sqlike:"$Key,comment=Primary key"`
    30  	Key           *types.Key
    31  	PtrUUID       *uuid.UUID
    32  	VirtualColumn string `sqlike:",generated_column"`
    33  	Date          civil.Date
    34  	SID           string
    35  	Emoji         string `sqlike:""`
    36  	FullText      string
    37  	LongStr       string  `sqlike:",longtext"`
    38  	CustomStrType LongStr `sqlike:",size=300"`
    39  	EmptyByte     []byte
    40  	Byte          []byte
    41  	Bool          bool
    42  	priv          int
    43  	Skip          interface{} `sqlike:"-"`
    44  	Int           int         `sqlike:",default=100"`
    45  	TinyInt       int8
    46  	SmallInt      int16
    47  	MediumInt     int32
    48  	BigInt        int64
    49  	Uint          uint
    50  	TinyUint      uint8
    51  	SmallUint     uint16
    52  	MediumUint    uint32
    53  	BigUint       uint64
    54  	Float32       float32
    55  	Float64       float64
    56  	UFloat32      float32 `sqlike:",unsigned"`
    57  	EmptyStruct   struct{}
    58  	Struct        struct {
    59  		Key           *types.Key
    60  		VirtualStr    string `sqlike:",virtual_column=VirtualColumn"`
    61  		StoredStr     string `sqlike:",stored_column"`
    62  		NestedBool    bool
    63  		NestedNullInt *int
    64  	}
    65  	JSONRaw    json.RawMessage
    66  	Map        map[string]int
    67  	DateTime   time.Time `sqlike:",size=0"`
    68  	Timestamp  time.Time
    69  	Location   *time.Location
    70  	Language   language.Tag
    71  	Languages  []language.Tag
    72  	Currency   currency.Unit
    73  	Currencies []currency.Unit
    74  	Enum       Enum      `sqlike:",enum=SUCCESS|FAILED|UNKNOWN"`
    75  	Set        types.Set `sqlike:",set=A|B|C"`
    76  	Model
    77  }
    78  
    79  type simpleStruct struct {
    80  	ID    int64 `sqlike:",auto_increment"`
    81  	Email string
    82  	Name  string
    83  	Age   uint16
    84  }
    85  
    86  type jsonStruct struct {
    87  	ID     int64 `sqlike:"$Key,auto_increment"`
    88  	Text   string
    89  	Raw    json.RawMessage
    90  	StrArr []string
    91  	IntArr []int
    92  	Map    map[string]int
    93  	Struct struct {
    94  		StringSlice sort.StringSlice
    95  		IntSlice    sort.IntSlice
    96  	}
    97  	NullableFloat *float64
    98  }
    99  
   100  // LongStr :
   101  type LongStr string
   102  
   103  // Country :
   104  type Country struct {
   105  	Name LongStr `sqlike:""`
   106  	Code string  `sqlike:""`
   107  }
   108  
   109  // Address :
   110  type Address struct {
   111  	Line1 string
   112  	Line2 string `sqlike:",virtual_column"` // this will not work if it's embedded struct
   113  	City  string `sqlike:",virtual_column"` // this will not work if it's embedded struct
   114  	State string `sqlike:",virtual_column"` // this will not work if it's embedded struct
   115  	// Country `sqlike:",inline"`
   116  	Country Country
   117  }
   118  
   119  // Enum :
   120  type Enum string
   121  
   122  // enum :
   123  const (
   124  	Success Enum = "SUCCESS"
   125  	Failed  Enum = "FAILED"
   126  	Unknown Enum = "UNKNOWN"
   127  )
   128  
   129  type model struct {
   130  	No int64
   131  	ID string `sqlike:"id"`
   132  	Address
   133  }
   134  
   135  type ptrStruct struct {
   136  	ID            int64 `sqlike:"$Key,auto_increment"`
   137  	NullUUID      *uuid.UUID
   138  	NullStr       *string `sqlike:"nullstr"`
   139  	NullBool      *bool
   140  	NullByte      *[]byte
   141  	NullInt       *int
   142  	NullInt8      *int8
   143  	NullInt16     *int16
   144  	NullInt32     *int32
   145  	NullInt64     *int64
   146  	NullUint      *uint
   147  	NullUint8     *uint8
   148  	NullUint16    *uint16
   149  	NullUint32    *uint32
   150  	NullUint64    *uint64
   151  	NullUFloat    *float32 `sqlike:",unsigned"`
   152  	NullFloat32   *float32
   153  	NullFloat64   *float64
   154  	NullStruct    *struct{}
   155  	NullJSONRaw   *json.RawMessage
   156  	NullTimestamp *time.Time
   157  	NullLocation  *time.Location
   158  	NullKey       *types.Key
   159  	NullDate      *civil.Date
   160  	NullTime      *civil.Time
   161  	NullEnum      *Enum `sqlike:",enum=SUCCESS|FAILED|UNKNOWN"`
   162  }
   163  
   164  type generatedStruct struct {
   165  	ID     string  `sqlike:"NestedID,generated_column"`
   166  	Amount float64 `sqlike:"Amount,generated_column"`
   167  	Nested struct {
   168  		ID     string  `sqlike:",stored_column=NestedID"`
   169  		Amount float64 `sqlike:",virtual_column=Amount"`
   170  	}
   171  	CivilDate civil.Date
   172  	model
   173  	Model `sqlike:"Date"`
   174  }
   175  
   176  type overrideStruct struct {
   177  	generatedStruct
   178  	ID     int64  `sqlike:",comment=Int64 ID"`      // override string ID of generatedStruct
   179  	Amount int    `sqlike:",comment=Int Amount"`    // override string Amount of generatedStruct
   180  	Nested string `sqlike:",comment=String Nested"` // override string Nested of generatedStruct
   181  }
   182  
   183  func newNormalStruct() normalStruct {
   184  	now := time.Now()
   185  	ns := normalStruct{}
   186  	// ns.Key = types.IDKey("NormalStruct", id, nil)
   187  	ns.ID = uuid.New()
   188  	ns.priv = 100
   189  	ns.Emoji = `๐Ÿ˜€ ๐Ÿ˜ ๐Ÿ˜‚ ๐Ÿคฃ ๐Ÿ˜ƒ ๐Ÿ˜„ ๐Ÿ˜… ๐Ÿ˜† ๐Ÿ˜‰ ๐Ÿ˜Š`
   190  	ns.Byte = []byte(`-----BEGIN PUBLIC KEY-----
   191  MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCklQio4TeIZo63S0FvNonY2/nA
   192  ZUvrnDRPIzEKK4A7Hu4UjxNhebxuEA/PqSJgxOIHVPnASrSwj+IlPokcdrR6Ekyn
   193  0cvjjwjGRyAGawVhf7TWHjkxTK6pIIqRiBK4h+E/fPwpvJTieFCSmIWovR8Wz6Jy
   194  eCnpmNrTzG6ZJlJcvQIDAQAB
   195  -----END PUBLIC KEY-----`)
   196  	ns.CustomStrType = LongStr(gofakeit.RandString([]string{
   197  		`่ฆšใˆใ‚‰ใ‚Œใชใใฆไฝฟใ†ใŸใณใซใ‚ฐใ‚ฐใฃใฆใ—ใพใ†ใฎใงใ€ไปฅๅพŒๆฅฝใ‚’ใ™ใ‚‹ใŸใ‚ใซใ‚นใƒ‹ใƒšใƒƒใƒˆใ‚’่จ˜ใ™ใ€‚`,
   198  		`ใ“ใฎใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใŒใงใใŸ่ƒŒๆ™ฏใฏ`,
   199  		`ใ“ใฎ่จ˜ไบ‹ใงใฏerrorsใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใฎไป•ๆง˜ใ‚’็ดนไป‹ใ—ใพใ™ใ€‚`,
   200  		`errors.Newใงไฝœๆˆใ—ใŸใ‚จใƒฉใƒผใฏใ€%+v ใฎใจใใซใƒ•ใ‚กใ‚คใƒซๅใ‚„ใƒกใ‚ฝใƒƒใƒ‰ๅใ‚’่กจ็คบใ—ใพใ™ใ€‚`,
   201  	}))
   202  	ns.LongStr = gofakeit.Sentence(50)
   203  	ns.Key = types.NewNameKey("Name", types.NewIDKey("ID", nil))
   204  	ns.Bool = true
   205  	ns.FullText = "Hal%o%()#$\\%^&_"
   206  	ns.Int = gofakeit.Number(100, 99999999)
   207  	ns.TinyInt = 99
   208  	ns.SmallInt = gofakeit.Int16()
   209  	ns.MediumInt = gofakeit.Int32()
   210  	ns.BigInt = gofakeit.Int64()
   211  	ns.TinyUint = gofakeit.Uint8()
   212  	ns.SmallUint = gofakeit.Uint16()
   213  	ns.MediumUint = gofakeit.Uint32()
   214  	ns.Uint = uint(gofakeit.Number(100, 99999999))
   215  	ns.BigUint = gofakeit.Uint64()
   216  	ns.UFloat32 = gofakeit.Float32Range(10, 10000)
   217  	ns.Float32 = gofakeit.Float32()
   218  	ns.Float64 = gofakeit.Float64()
   219  	ns.JSONRaw = json.RawMessage(`{
   220  		"message" :  "hello world",
   221  		"code":      200,
   222  		"error": {
   223  			"code": "Unauthorised",
   224  			"message": "please contact our support"
   225  		}
   226  	}`)
   227  	ns.Struct.VirtualStr = gofakeit.Sentence(10)
   228  	ns.Struct.StoredStr = `hello world!`
   229  	ns.Struct.NestedBool = true
   230  	ns.Date = civil.DateOf(now)
   231  	ns.DateTime = now
   232  	ns.Location, _ = time.LoadLocation("Asia/Kuala_Lumpur")
   233  	ns.Timestamp = now
   234  	ns.Language = language.English
   235  	ns.Currencies = []currency.Unit{
   236  		currency.AUD,
   237  		currency.EUR,
   238  	}
   239  	ns.Enum = Enum(gofakeit.RandString([]string{
   240  		"SUCCESS",
   241  		"FAILED",
   242  		"UNKNOWN",
   243  	}))
   244  	ns.CreatedAt = now
   245  	ns.UpdatedAt = now
   246  	return ns
   247  }
   248  
   249  func newPtrStruct() ptrStruct {
   250  	now := time.Now()
   251  	str := `hello world`
   252  	uid := uuid.New()
   253  	flag := true
   254  	b := []byte(`hello world`)
   255  	date, _ := civil.ParseDate("2019-01-02")
   256  	jsonByte := json.RawMessage(`{"message":"hello world"}`)
   257  	i := 124
   258  	i32 := int32(-603883)
   259  	i64 := int64(-3712897389712688393)
   260  	u8 := uint8(88)
   261  	u64 := uint64(37128973897126)
   262  	enum := Success
   263  	dt := civil.DateOf(now)
   264  	t := civil.TimeOf(now)
   265  
   266  	ps := ptrStruct{}
   267  	ps.NullStr = &str
   268  	ps.NullUUID = &uid
   269  	ps.NullByte = &b
   270  	ps.NullBool = &flag
   271  	ps.NullInt = &i
   272  	ps.NullInt32 = &i32
   273  	ps.NullInt64 = &i64
   274  	ps.NullDate = &date
   275  	ps.NullUint8 = &u8
   276  	ps.NullUint64 = &u64
   277  	ps.NullJSONRaw = &jsonByte
   278  	ps.NullDate = &dt
   279  	ps.NullTime = &t
   280  	ps.NullTimestamp = &now
   281  	ps.NullEnum = &enum
   282  	return ps
   283  }
   284  
   285  func newGeneratedStruct() *generatedStruct {
   286  	utcNow := time.Now().UTC()
   287  	gs := &generatedStruct{}
   288  	gs.Nested.ID = uuid.New().String()
   289  	gs.Nested.Amount = gofakeit.Float64Range(1, 10000)
   290  	gs.CivilDate = civil.DateOf(utcNow)
   291  	gs.CreatedAt = utcNow
   292  	gs.UpdatedAt = utcNow
   293  	return gs
   294  }