Serge Bazanski | cc25bdf | 2018-10-25 14:02:58 +0200 | [diff] [blame] | 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 | |
| 15 | package strfmt |
| 16 | |
| 17 | import ( |
| 18 | "database/sql/driver" |
| 19 | "errors" |
| 20 | "fmt" |
| 21 | |
| 22 | "github.com/globalsign/mgo/bson" |
| 23 | "github.com/mailru/easyjson/jlexer" |
| 24 | "github.com/mailru/easyjson/jwriter" |
| 25 | ) |
| 26 | |
| 27 | func init() { |
| 28 | var id ObjectId |
| 29 | // register this format in the default registry |
| 30 | Default.Add("bsonobjectid", &id, IsBSONObjectID) |
| 31 | } |
| 32 | |
| 33 | // IsBSONObjectID returns true when the string is a valid BSON.ObjectId |
| 34 | func IsBSONObjectID(str string) bool { |
| 35 | return bson.IsObjectIdHex(str) |
| 36 | } |
| 37 | |
| 38 | // ObjectId represents a BSON object ID (alias to github.com/globalsign/mgo/bson.ObjectId) |
| 39 | // |
| 40 | // swagger:strfmt bsonobjectid |
| 41 | type ObjectId bson.ObjectId |
| 42 | |
| 43 | // NewObjectId creates a ObjectId from a Hex String |
| 44 | func NewObjectId(hex string) ObjectId { |
| 45 | return ObjectId(bson.ObjectIdHex(hex)) |
| 46 | } |
| 47 | |
| 48 | // MarshalText turns this instance into text |
| 49 | func (id *ObjectId) MarshalText() ([]byte, error) { |
| 50 | return []byte(bson.ObjectId(*id).Hex()), nil |
| 51 | } |
| 52 | |
| 53 | // UnmarshalText hydrates this instance from text |
| 54 | func (id *ObjectId) UnmarshalText(data []byte) error { // validation is performed later on |
| 55 | *id = ObjectId(bson.ObjectIdHex(string(data))) |
| 56 | return nil |
| 57 | } |
| 58 | |
| 59 | // Scan read a value from a database driver |
| 60 | func (id *ObjectId) Scan(raw interface{}) error { |
| 61 | var data []byte |
| 62 | switch v := raw.(type) { |
| 63 | case []byte: |
| 64 | data = v |
| 65 | case string: |
| 66 | data = []byte(v) |
| 67 | default: |
| 68 | return fmt.Errorf("cannot sql.Scan() strfmt.URI from: %#v", v) |
| 69 | } |
| 70 | |
| 71 | return id.UnmarshalText(data) |
| 72 | } |
| 73 | |
| 74 | // Value converts a value to a database driver value |
| 75 | func (id *ObjectId) Value() (driver.Value, error) { |
| 76 | return driver.Value(string(*id)), nil |
| 77 | } |
| 78 | |
| 79 | func (id *ObjectId) String() string { |
| 80 | return string(*id) |
| 81 | } |
| 82 | |
| 83 | // MarshalJSON returns the ObjectId as JSON |
| 84 | func (id *ObjectId) MarshalJSON() ([]byte, error) { |
| 85 | var w jwriter.Writer |
| 86 | id.MarshalEasyJSON(&w) |
| 87 | return w.BuildBytes() |
| 88 | } |
| 89 | |
| 90 | // MarshalEasyJSON writes the ObjectId to a easyjson.Writer |
| 91 | func (id *ObjectId) MarshalEasyJSON(w *jwriter.Writer) { |
| 92 | w.String(bson.ObjectId(*id).Hex()) |
| 93 | } |
| 94 | |
| 95 | // UnmarshalJSON sets the ObjectId from JSON |
| 96 | func (id *ObjectId) UnmarshalJSON(data []byte) error { |
| 97 | l := jlexer.Lexer{Data: data} |
| 98 | id.UnmarshalEasyJSON(&l) |
| 99 | return l.Error() |
| 100 | } |
| 101 | |
| 102 | // UnmarshalEasyJSON sets the ObjectId from a easyjson.Lexer |
| 103 | func (id *ObjectId) UnmarshalEasyJSON(in *jlexer.Lexer) { |
| 104 | if data := in.String(); in.Ok() { |
| 105 | *id = NewObjectId(data) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // GetBSON returns the hex representation of the ObjectId as a bson.M{} map. |
| 110 | func (id *ObjectId) GetBSON() (interface{}, error) { |
| 111 | return bson.M{"data": bson.ObjectId(*id).Hex()}, nil |
| 112 | } |
| 113 | |
| 114 | // SetBSON sets the ObjectId from raw bson data |
| 115 | func (id *ObjectId) SetBSON(raw bson.Raw) error { |
| 116 | var m bson.M |
| 117 | if err := raw.Unmarshal(&m); err != nil { |
| 118 | return err |
| 119 | } |
| 120 | |
| 121 | if data, ok := m["data"].(string); ok { |
| 122 | *id = NewObjectId(data) |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | return errors.New("couldn't unmarshal bson raw value as ObjectId") |
| 127 | } |