date.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2015 go-swagger maintainers
  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. package strfmt
  15. import (
  16. "database/sql/driver"
  17. "errors"
  18. "fmt"
  19. "time"
  20. "github.com/globalsign/mgo/bson"
  21. "github.com/mailru/easyjson/jlexer"
  22. "github.com/mailru/easyjson/jwriter"
  23. )
  24. func init() {
  25. d := Date{}
  26. // register this format in the default registry
  27. Default.Add("date", &d, IsDate)
  28. }
  29. // IsDate returns true when the string is a valid date
  30. func IsDate(str string) bool {
  31. _, err := time.Parse(RFC3339FullDate, str)
  32. return err == nil
  33. }
  34. const (
  35. // RFC3339FullDate represents a full-date as specified by RFC3339
  36. // See: http://goo.gl/xXOvVd
  37. RFC3339FullDate = "2006-01-02"
  38. )
  39. // Date represents a date from the API
  40. //
  41. // swagger:strfmt date
  42. type Date time.Time
  43. // String converts this date into a string
  44. func (d Date) String() string {
  45. return time.Time(d).Format(RFC3339FullDate)
  46. }
  47. // UnmarshalText parses a text representation into a date type
  48. func (d *Date) UnmarshalText(text []byte) error {
  49. if len(text) == 0 {
  50. return nil
  51. }
  52. dd, err := time.Parse(RFC3339FullDate, string(text))
  53. if err != nil {
  54. return err
  55. }
  56. *d = Date(dd)
  57. return nil
  58. }
  59. // MarshalText serializes this date type to string
  60. func (d Date) MarshalText() ([]byte, error) {
  61. return []byte(d.String()), nil
  62. }
  63. // Scan scans a Date value from database driver type.
  64. func (d *Date) Scan(raw interface{}) error {
  65. switch v := raw.(type) {
  66. case []byte:
  67. return d.UnmarshalText(v)
  68. case string:
  69. return d.UnmarshalText([]byte(v))
  70. case time.Time:
  71. *d = Date(v)
  72. return nil
  73. case nil:
  74. *d = Date{}
  75. return nil
  76. default:
  77. return fmt.Errorf("cannot sql.Scan() strfmt.Date from: %#v", v)
  78. }
  79. }
  80. // Value converts Date to a primitive value ready to written to a database.
  81. func (d Date) Value() (driver.Value, error) {
  82. return driver.Value(d.String()), nil
  83. }
  84. // MarshalJSON returns the Date as JSON
  85. func (d Date) MarshalJSON() ([]byte, error) {
  86. var w jwriter.Writer
  87. d.MarshalEasyJSON(&w)
  88. return w.BuildBytes()
  89. }
  90. // MarshalEasyJSON writes the Date to a easyjson.Writer
  91. func (d Date) MarshalEasyJSON(w *jwriter.Writer) {
  92. w.String(time.Time(d).Format(RFC3339FullDate))
  93. }
  94. // UnmarshalJSON sets the Date from JSON
  95. func (d *Date) UnmarshalJSON(data []byte) error {
  96. if string(data) == jsonNull {
  97. return nil
  98. }
  99. l := jlexer.Lexer{Data: data}
  100. d.UnmarshalEasyJSON(&l)
  101. return l.Error()
  102. }
  103. // UnmarshalEasyJSON sets the Date from a easyjson.Lexer
  104. func (d *Date) UnmarshalEasyJSON(in *jlexer.Lexer) {
  105. if data := in.String(); in.Ok() {
  106. tt, err := time.Parse(RFC3339FullDate, data)
  107. if err != nil {
  108. in.AddError(err)
  109. return
  110. }
  111. *d = Date(tt)
  112. }
  113. }
  114. // GetBSON returns the Date as a bson.M{} map.
  115. func (d *Date) GetBSON() (interface{}, error) {
  116. return bson.M{"data": d.String()}, nil
  117. }
  118. // SetBSON sets the Date from raw bson data
  119. func (d *Date) SetBSON(raw bson.Raw) error {
  120. var m bson.M
  121. if err := raw.Unmarshal(&m); err != nil {
  122. return err
  123. }
  124. if data, ok := m["data"].(string); ok {
  125. rd, err := time.Parse(RFC3339FullDate, data)
  126. *d = Date(rd)
  127. return err
  128. }
  129. return errors.New("couldn't unmarshal bson raw value as Date")
  130. }