github.com/astaxie/beego@v1.12.3/orm/models_fields.go (about)

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