k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/strfmt/bson.go (about) 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 bsonprim "k8s.io/kube-openapi/pkg/validation/strfmt/bson" 19 ) 20 21 func init() { 22 var id ObjectId 23 // register this format in the default registry 24 Default.Add("bsonobjectid", &id, IsBSONObjectID) 25 } 26 27 // IsBSONObjectID returns true when the string is a valid BSON.ObjectId 28 func IsBSONObjectID(str string) bool { 29 _, err := bsonprim.ObjectIDFromHex(str) 30 return err == nil 31 } 32 33 // ObjectId represents a BSON object ID (alias to go.mongodb.org/mongo-driver/bson/primitive.ObjectID) 34 // 35 // swagger:strfmt bsonobjectid 36 type ObjectId bsonprim.ObjectID 37 38 // NewObjectId creates a ObjectId from a Hex String 39 func NewObjectId(hex string) ObjectId { 40 oid, err := bsonprim.ObjectIDFromHex(hex) 41 if err != nil { 42 panic(err) 43 } 44 return ObjectId(oid) 45 } 46 47 // MarshalText turns this instance into text 48 func (id ObjectId) MarshalText() ([]byte, error) { 49 oid := bsonprim.ObjectID(id) 50 if oid == bsonprim.NilObjectID { 51 return nil, nil 52 } 53 return []byte(oid.Hex()), nil 54 } 55 56 // UnmarshalText hydrates this instance from text 57 func (id *ObjectId) UnmarshalText(data []byte) error { // validation is performed later on 58 if len(data) == 0 { 59 *id = ObjectId(bsonprim.NilObjectID) 60 return nil 61 } 62 oidstr := string(data) 63 oid, err := bsonprim.ObjectIDFromHex(oidstr) 64 if err != nil { 65 return err 66 } 67 *id = ObjectId(oid) 68 return nil 69 } 70 71 func (id ObjectId) String() string { 72 return bsonprim.ObjectID(id).String() 73 } 74 75 // MarshalJSON returns the ObjectId as JSON 76 func (id ObjectId) MarshalJSON() ([]byte, error) { 77 return bsonprim.ObjectID(id).MarshalJSON() 78 } 79 80 // UnmarshalJSON sets the ObjectId from JSON 81 func (id *ObjectId) UnmarshalJSON(data []byte) error { 82 var obj bsonprim.ObjectID 83 if err := obj.UnmarshalJSON(data); err != nil { 84 return err 85 } 86 *id = ObjectId(obj) 87 return nil 88 } 89 90 // DeepCopyInto copies the receiver and writes its value into out. 91 func (id *ObjectId) DeepCopyInto(out *ObjectId) { 92 *out = *id 93 } 94 95 // DeepCopy copies the receiver into a new ObjectId. 96 func (id *ObjectId) DeepCopy() *ObjectId { 97 if id == nil { 98 return nil 99 } 100 out := new(ObjectId) 101 id.DeepCopyInto(out) 102 return out 103 }