github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/databases/orm/models_fields.go (about) 1 // The original package is migrated from beego and modified, you can find orignal from following link: 2 // "github.com/beego/beego/" 3 // 4 // Copyright 2023 IAC. All Rights Reserved. 5 // 6 // Licensed under the Apache License, Version 2.0 (the "License"); 7 // you may not use this file except in compliance with the License. 8 // You may obtain a copy of the License at 9 // 10 // http://www.apache.org/licenses/LICENSE-2.0 11 // 12 // Unless required by applicable law or agreed to in writing, software 13 // distributed under the License is distributed on an "AS IS" BASIS, 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 18 package orm 19 20 import ( 21 "fmt" 22 "strconv" 23 "time" 24 ) 25 26 // Define the Type enum 27 const ( 28 TypeBooleanField = 1 << iota 29 TypeVarCharField 30 TypeCharField 31 TypeTextField 32 TypeTimeField 33 TypeDateField 34 TypeDateTimeField 35 TypeBitField 36 TypeSmallIntegerField 37 TypeIntegerField 38 TypeBigIntegerField 39 TypePositiveBitField 40 TypePositiveSmallIntegerField 41 TypePositiveIntegerField 42 TypePositiveBigIntegerField 43 TypeFloatField 44 TypeDecimalField 45 TypeJSONField 46 TypeJsonbField 47 RelForeignKey 48 RelOneToOne 49 RelManyToMany 50 RelReverseOne 51 RelReverseMany 52 ) 53 54 // Define some logic enum 55 const ( 56 IsIntegerField = ^-TypePositiveBigIntegerField >> 6 << 7 57 IsPositiveIntegerField = ^-TypePositiveBigIntegerField >> 10 << 11 58 IsRelField = ^-RelReverseMany >> 18 << 19 59 IsFieldType = ^-RelReverseMany<<1 + 1 60 ) 61 62 // BooleanField A true/false field. 63 type BooleanField bool 64 65 // Value return the BooleanField 66 func (e BooleanField) Value() bool { 67 return bool(e) 68 } 69 70 // Set will set the BooleanField 71 func (e *BooleanField) Set(d bool) { 72 *e = BooleanField(d) 73 } 74 75 // String format the Bool to string 76 func (e *BooleanField) String() string { 77 return strconv.FormatBool(e.Value()) 78 } 79 80 // FieldType return BooleanField the type 81 func (e *BooleanField) FieldType() int { 82 return TypeBooleanField 83 } 84 85 // SetRaw set the interface to bool 86 func (e *BooleanField) SetRaw(value interface{}) error { 87 switch d := value.(type) { 88 case bool: 89 e.Set(d) 90 case string: 91 v, err := StrTo(d).Bool() 92 if err == nil { 93 e.Set(v) 94 } 95 return err 96 default: 97 return fmt.Errorf("<BooleanField.SetRaw> unknown value `%s`", value) 98 } 99 return nil 100 } 101 102 // RawValue return the current value 103 func (e *BooleanField) RawValue() interface{} { 104 return e.Value() 105 } 106 107 // verify the BooleanField implement the Fielder interface 108 var _ Fielder = new(BooleanField) 109 110 // CharField A string field 111 // required values tag: size 112 // The size is enforced at the database level and in models’s validation. 113 // eg: `orm:"size(120)"` 114 type CharField string 115 116 // Value return the CharField's Value 117 func (e CharField) Value() string { 118 return string(e) 119 } 120 121 // Set CharField value 122 func (e *CharField) Set(d string) { 123 *e = CharField(d) 124 } 125 126 // String return the CharField 127 func (e *CharField) String() string { 128 return e.Value() 129 } 130 131 // FieldType return the enum type 132 func (e *CharField) FieldType() int { 133 return TypeVarCharField 134 } 135 136 // SetRaw set the interface to string 137 func (e *CharField) SetRaw(value interface{}) error { 138 switch d := value.(type) { 139 case string: 140 e.Set(d) 141 default: 142 return fmt.Errorf("<CharField.SetRaw> unknown value `%s`", value) 143 } 144 return nil 145 } 146 147 // RawValue return the CharField value 148 func (e *CharField) RawValue() interface{} { 149 return e.Value() 150 } 151 152 // verify CharField implement Fielder 153 var _ Fielder = new(CharField) 154 155 // TimeField A time, represented in go by a time.Time instance. 156 // only time values like 10:00:00 157 // Has a few extra, optional attr tag: 158 // 159 // auto_now: 160 // Automatically set the field to now every time the object is saved. Useful for “last-modified” timestamps. 161 // Note that the current date is always used; it’s not just a default value that you can override. 162 // 163 // auto_now_add: 164 // Automatically set the field to now when the object is first created. Useful for creation of timestamps. 165 // Note that the current date is always used; it’s not just a default value that you can override. 166 // 167 // eg: `orm:"auto_now"` or `orm:"auto_now_add"` 168 type TimeField time.Time 169 170 // Value return the time.Time 171 func (e TimeField) Value() time.Time { 172 return time.Time(e) 173 } 174 175 // Set set the TimeField's value 176 func (e *TimeField) Set(d time.Time) { 177 *e = TimeField(d) 178 } 179 180 // String convert time to string 181 func (e *TimeField) String() string { 182 return e.Value().String() 183 } 184 185 // FieldType return enum type Date 186 func (e *TimeField) FieldType() int { 187 return TypeDateField 188 } 189 190 // SetRaw convert the interface to time.Time. Allow string and time.Time 191 func (e *TimeField) SetRaw(value interface{}) error { 192 switch d := value.(type) { 193 case time.Time: 194 e.Set(d) 195 case string: 196 v, err := timeParse(d, formatTime) 197 if err == nil { 198 e.Set(v) 199 } 200 return err 201 default: 202 return fmt.Errorf("<TimeField.SetRaw> unknown value `%s`", value) 203 } 204 return nil 205 } 206 207 // RawValue return time value 208 func (e *TimeField) RawValue() interface{} { 209 return e.Value() 210 } 211 212 var _ Fielder = new(TimeField) 213 214 // DateField A date, represented in go by a time.Time instance. 215 // only date values like 2006-01-02 216 // Has a few extra, optional attr tag: 217 // 218 // auto_now: 219 // Automatically set the field to now every time the object is saved. Useful for “last-modified” timestamps. 220 // Note that the current date is always used; it’s not just a default value that you can override. 221 // 222 // auto_now_add: 223 // Automatically set the field to now when the object is first created. Useful for creation of timestamps. 224 // Note that the current date is always used; it’s not just a default value that you can override. 225 // 226 // eg: `orm:"auto_now"` or `orm:"auto_now_add"` 227 type DateField time.Time 228 229 // Value return the time.Time 230 func (e DateField) Value() time.Time { 231 return time.Time(e) 232 } 233 234 // Set set the DateField's value 235 func (e *DateField) Set(d time.Time) { 236 *e = DateField(d) 237 } 238 239 // String convert datetime to string 240 func (e *DateField) String() string { 241 return e.Value().String() 242 } 243 244 // FieldType return enum type Date 245 func (e *DateField) FieldType() int { 246 return TypeDateField 247 } 248 249 // SetRaw convert the interface to time.Time. Allow string and time.Time 250 func (e *DateField) SetRaw(value interface{}) error { 251 switch d := value.(type) { 252 case time.Time: 253 e.Set(d) 254 case string: 255 v, err := timeParse(d, formatDate) 256 if err == nil { 257 e.Set(v) 258 } 259 return err 260 default: 261 return fmt.Errorf("<DateField.SetRaw> unknown value `%s`", value) 262 } 263 return nil 264 } 265 266 // RawValue return Date value 267 func (e *DateField) RawValue() interface{} { 268 return e.Value() 269 } 270 271 // verify DateField implement fielder interface 272 var _ Fielder = new(DateField) 273 274 // DateTimeField A date, represented in go by a time.Time instance. 275 // datetime values like 2006-01-02 15:04:05 276 // Takes the same extra arguments as DateField. 277 type DateTimeField time.Time 278 279 // Value return the datetime value 280 func (e DateTimeField) Value() time.Time { 281 return time.Time(e) 282 } 283 284 // Set set the time.Time to datetime 285 func (e *DateTimeField) Set(d time.Time) { 286 *e = DateTimeField(d) 287 } 288 289 // String return the time's String 290 func (e *DateTimeField) String() string { 291 return e.Value().String() 292 } 293 294 // FieldType return the enum TypeDateTimeField 295 func (e *DateTimeField) FieldType() int { 296 return TypeDateTimeField 297 } 298 299 // SetRaw convert the string or time.Time to DateTimeField 300 func (e *DateTimeField) SetRaw(value interface{}) error { 301 switch d := value.(type) { 302 case time.Time: 303 e.Set(d) 304 case string: 305 v, err := timeParse(d, formatDateTime) 306 if err == nil { 307 e.Set(v) 308 } 309 return err 310 default: 311 return fmt.Errorf("<DateTimeField.SetRaw> unknown value `%s`", value) 312 } 313 return nil 314 } 315 316 // RawValue return the datetime value 317 func (e *DateTimeField) RawValue() interface{} { 318 return e.Value() 319 } 320 321 // verify datetime implement fielder 322 var _ Fielder = new(DateTimeField) 323 324 // FloatField A floating-point number represented in go by a float32 value. 325 type FloatField float64 326 327 // Value return the FloatField value 328 func (e FloatField) Value() float64 { 329 return float64(e) 330 } 331 332 // Set the Float64 333 func (e *FloatField) Set(d float64) { 334 *e = FloatField(d) 335 } 336 337 // String return the string 338 func (e *FloatField) String() string { 339 return ToStr(e.Value(), -1, 32) 340 } 341 342 // FieldType return the enum type 343 func (e *FloatField) FieldType() int { 344 return TypeFloatField 345 } 346 347 // SetRaw converter interface Float64 float32 or string to FloatField 348 func (e *FloatField) SetRaw(value interface{}) error { 349 switch d := value.(type) { 350 case float32: 351 e.Set(float64(d)) 352 case float64: 353 e.Set(d) 354 case string: 355 v, err := StrTo(d).Float64() 356 if err == nil { 357 e.Set(v) 358 } 359 return err 360 default: 361 return fmt.Errorf("<FloatField.SetRaw> unknown value `%s`", value) 362 } 363 return nil 364 } 365 366 // RawValue return the FloatField value 367 func (e *FloatField) RawValue() interface{} { 368 return e.Value() 369 } 370 371 // verify FloatField implement Fielder 372 var _ Fielder = new(FloatField) 373 374 // SmallIntegerField -32768 to 32767 375 type SmallIntegerField int16 376 377 // Value return int16 value 378 func (e SmallIntegerField) Value() int16 { 379 return int16(e) 380 } 381 382 // Set the SmallIntegerField value 383 func (e *SmallIntegerField) Set(d int16) { 384 *e = SmallIntegerField(d) 385 } 386 387 // String convert smallint to string 388 func (e *SmallIntegerField) String() string { 389 return ToStr(e.Value()) 390 } 391 392 // FieldType return enum type SmallIntegerField 393 func (e *SmallIntegerField) FieldType() int { 394 return TypeSmallIntegerField 395 } 396 397 // SetRaw convert interface int16/string to int16 398 func (e *SmallIntegerField) SetRaw(value interface{}) error { 399 switch d := value.(type) { 400 case int16: 401 e.Set(d) 402 case string: 403 v, err := StrTo(d).Int16() 404 if err == nil { 405 e.Set(v) 406 } 407 return err 408 default: 409 return fmt.Errorf("<SmallIntegerField.SetRaw> unknown value `%s`", value) 410 } 411 return nil 412 } 413 414 // RawValue return smallint value 415 func (e *SmallIntegerField) RawValue() interface{} { 416 return e.Value() 417 } 418 419 // verify SmallIntegerField implement Fielder 420 var _ Fielder = new(SmallIntegerField) 421 422 // IntegerField -2147483648 to 2147483647 423 type IntegerField int32 424 425 // Value return the int32 426 func (e IntegerField) Value() int32 { 427 return int32(e) 428 } 429 430 // Set IntegerField value 431 func (e *IntegerField) Set(d int32) { 432 *e = IntegerField(d) 433 } 434 435 // String convert Int32 to string 436 func (e *IntegerField) String() string { 437 return ToStr(e.Value()) 438 } 439 440 // FieldType return the enum type 441 func (e *IntegerField) FieldType() int { 442 return TypeIntegerField 443 } 444 445 // SetRaw convert interface int32/string to int32 446 func (e *IntegerField) SetRaw(value interface{}) error { 447 switch d := value.(type) { 448 case int32: 449 e.Set(d) 450 case string: 451 v, err := StrTo(d).Int32() 452 if err == nil { 453 e.Set(v) 454 } 455 return err 456 default: 457 return fmt.Errorf("<IntegerField.SetRaw> unknown value `%s`", value) 458 } 459 return nil 460 } 461 462 // RawValue return IntegerField value 463 func (e *IntegerField) RawValue() interface{} { 464 return e.Value() 465 } 466 467 // verify IntegerField implement Fielder 468 var _ Fielder = new(IntegerField) 469 470 // BigIntegerField -9223372036854775808 to 9223372036854775807. 471 type BigIntegerField int64 472 473 // Value return int64 474 func (e BigIntegerField) Value() int64 { 475 return int64(e) 476 } 477 478 // Set the BigIntegerField value 479 func (e *BigIntegerField) Set(d int64) { 480 *e = BigIntegerField(d) 481 } 482 483 // String convert BigIntegerField to string 484 func (e *BigIntegerField) String() string { 485 return ToStr(e.Value()) 486 } 487 488 // FieldType return enum type 489 func (e *BigIntegerField) FieldType() int { 490 return TypeBigIntegerField 491 } 492 493 // SetRaw convert interface int64/string to int64 494 func (e *BigIntegerField) SetRaw(value interface{}) error { 495 switch d := value.(type) { 496 case int64: 497 e.Set(d) 498 case string: 499 v, err := StrTo(d).Int64() 500 if err == nil { 501 e.Set(v) 502 } 503 return err 504 default: 505 return fmt.Errorf("<BigIntegerField.SetRaw> unknown value `%s`", value) 506 } 507 return nil 508 } 509 510 // RawValue return BigIntegerField value 511 func (e *BigIntegerField) RawValue() interface{} { 512 return e.Value() 513 } 514 515 // verify BigIntegerField implement Fielder 516 var _ Fielder = new(BigIntegerField) 517 518 // PositiveSmallIntegerField 0 to 65535 519 type PositiveSmallIntegerField uint16 520 521 // Value return uint16 522 func (e PositiveSmallIntegerField) Value() uint16 { 523 return uint16(e) 524 } 525 526 // Set PositiveSmallIntegerField value 527 func (e *PositiveSmallIntegerField) Set(d uint16) { 528 *e = PositiveSmallIntegerField(d) 529 } 530 531 // String convert uint16 to string 532 func (e *PositiveSmallIntegerField) String() string { 533 return ToStr(e.Value()) 534 } 535 536 // FieldType return enum type 537 func (e *PositiveSmallIntegerField) FieldType() int { 538 return TypePositiveSmallIntegerField 539 } 540 541 // SetRaw convert Interface uint16/string to uint16 542 func (e *PositiveSmallIntegerField) SetRaw(value interface{}) error { 543 switch d := value.(type) { 544 case uint16: 545 e.Set(d) 546 case string: 547 v, err := StrTo(d).Uint16() 548 if err == nil { 549 e.Set(v) 550 } 551 return err 552 default: 553 return fmt.Errorf("<PositiveSmallIntegerField.SetRaw> unknown value `%s`", value) 554 } 555 return nil 556 } 557 558 // RawValue returns PositiveSmallIntegerField value 559 func (e *PositiveSmallIntegerField) RawValue() interface{} { 560 return e.Value() 561 } 562 563 // verify PositiveSmallIntegerField implement Fielder 564 var _ Fielder = new(PositiveSmallIntegerField) 565 566 // PositiveIntegerField 0 to 4294967295 567 type PositiveIntegerField uint32 568 569 // Value return PositiveIntegerField value. Uint32 570 func (e PositiveIntegerField) Value() uint32 { 571 return uint32(e) 572 } 573 574 // Set the PositiveIntegerField value 575 func (e *PositiveIntegerField) Set(d uint32) { 576 *e = PositiveIntegerField(d) 577 } 578 579 // String convert PositiveIntegerField to string 580 func (e *PositiveIntegerField) String() string { 581 return ToStr(e.Value()) 582 } 583 584 // FieldType return enum type 585 func (e *PositiveIntegerField) FieldType() int { 586 return TypePositiveIntegerField 587 } 588 589 // SetRaw convert interface uint32/string to Uint32 590 func (e *PositiveIntegerField) SetRaw(value interface{}) error { 591 switch d := value.(type) { 592 case uint32: 593 e.Set(d) 594 case string: 595 v, err := StrTo(d).Uint32() 596 if err == nil { 597 e.Set(v) 598 } 599 return err 600 default: 601 return fmt.Errorf("<PositiveIntegerField.SetRaw> unknown value `%s`", value) 602 } 603 return nil 604 } 605 606 // RawValue return the PositiveIntegerField Value 607 func (e *PositiveIntegerField) RawValue() interface{} { 608 return e.Value() 609 } 610 611 // verify PositiveIntegerField implement Fielder 612 var _ Fielder = new(PositiveIntegerField) 613 614 // PositiveBigIntegerField 0 to 18446744073709551615 615 type PositiveBigIntegerField uint64 616 617 // Value return uint64 618 func (e PositiveBigIntegerField) Value() uint64 { 619 return uint64(e) 620 } 621 622 // Set PositiveBigIntegerField value 623 func (e *PositiveBigIntegerField) Set(d uint64) { 624 *e = PositiveBigIntegerField(d) 625 } 626 627 // String convert PositiveBigIntegerField to string 628 func (e *PositiveBigIntegerField) String() string { 629 return ToStr(e.Value()) 630 } 631 632 // FieldType return enum type 633 func (e *PositiveBigIntegerField) FieldType() int { 634 return TypePositiveIntegerField 635 } 636 637 // SetRaw convert interface uint64/string to Uint64 638 func (e *PositiveBigIntegerField) SetRaw(value interface{}) error { 639 switch d := value.(type) { 640 case uint64: 641 e.Set(d) 642 case string: 643 v, err := StrTo(d).Uint64() 644 if err == nil { 645 e.Set(v) 646 } 647 return err 648 default: 649 return fmt.Errorf("<PositiveBigIntegerField.SetRaw> unknown value `%s`", value) 650 } 651 return nil 652 } 653 654 // RawValue return PositiveBigIntegerField value 655 func (e *PositiveBigIntegerField) RawValue() interface{} { 656 return e.Value() 657 } 658 659 // verify PositiveBigIntegerField implement Fielder 660 var _ Fielder = new(PositiveBigIntegerField) 661 662 // TextField A large text field. 663 type TextField string 664 665 // Value return TextField value 666 func (e TextField) Value() string { 667 return string(e) 668 } 669 670 // Set the TextField value 671 func (e *TextField) Set(d string) { 672 *e = TextField(d) 673 } 674 675 // String convert TextField to string 676 func (e *TextField) String() string { 677 return e.Value() 678 } 679 680 // FieldType return enum type 681 func (e *TextField) FieldType() int { 682 return TypeTextField 683 } 684 685 // SetRaw convert interface string to string 686 func (e *TextField) SetRaw(value interface{}) error { 687 switch d := value.(type) { 688 case string: 689 e.Set(d) 690 default: 691 return fmt.Errorf("<TextField.SetRaw> unknown value `%s`", value) 692 } 693 return nil 694 } 695 696 // RawValue return TextField value 697 func (e *TextField) RawValue() interface{} { 698 return e.Value() 699 } 700 701 // verify TextField implement Fielder 702 var _ Fielder = new(TextField) 703 704 // JSONField postgres json field. 705 type JSONField string 706 707 // Value return JSONField value 708 func (j JSONField) Value() string { 709 return string(j) 710 } 711 712 // Set the JSONField value 713 func (j *JSONField) Set(d string) { 714 *j = JSONField(d) 715 } 716 717 // String convert JSONField to string 718 func (j *JSONField) String() string { 719 return j.Value() 720 } 721 722 // FieldType return enum type 723 func (j *JSONField) FieldType() int { 724 return TypeJSONField 725 } 726 727 // SetRaw convert interface string to string 728 func (j *JSONField) SetRaw(value interface{}) error { 729 switch d := value.(type) { 730 case string: 731 j.Set(d) 732 default: 733 return fmt.Errorf("<JSONField.SetRaw> unknown value `%s`", value) 734 } 735 return nil 736 } 737 738 // RawValue return JSONField value 739 func (j *JSONField) RawValue() interface{} { 740 return j.Value() 741 } 742 743 // verify JSONField implement Fielder 744 var _ Fielder = new(JSONField) 745 746 // JsonbField postgres json field. 747 type JsonbField string 748 749 // Value return JsonbField value 750 func (j JsonbField) Value() string { 751 return string(j) 752 } 753 754 // Set the JsonbField value 755 func (j *JsonbField) Set(d string) { 756 *j = JsonbField(d) 757 } 758 759 // String convert JsonbField to string 760 func (j *JsonbField) String() string { 761 return j.Value() 762 } 763 764 // FieldType return enum type 765 func (j *JsonbField) FieldType() int { 766 return TypeJsonbField 767 } 768 769 // SetRaw convert interface string to string 770 func (j *JsonbField) SetRaw(value interface{}) error { 771 switch d := value.(type) { 772 case string: 773 j.Set(d) 774 default: 775 return fmt.Errorf("<JsonbField.SetRaw> unknown value `%s`", value) 776 } 777 return nil 778 } 779 780 // RawValue return JsonbField value 781 func (j *JsonbField) RawValue() interface{} { 782 return j.Value() 783 } 784 785 // verify JsonbField implement Fielder 786 var _ Fielder = new(JsonbField)