Ejemplo n.º 1
0
Archivo: bson.go Proyecto: h12w/uuid
func (id *UUID) SetBSON(raw bson.Raw) error {
	var s string
	if err := raw.Unmarshal(&s); err != nil {
		return err
	}
	return id.UnmarshalText([]byte(s))
}
Ejemplo n.º 2
0
func (t *TimePeriod) SetBSON(raw bson.Raw) error {
	var str string
	err := raw.Unmarshal(&str)
	if err == nil {
		str = strings.Replace(str, "\"", "", -1)
		split := strings.Split(str, "/")

		if len(split) == 2 {
			t.FromTime, err = time.Parse(STD_TIME_FORMAT_PERIOD, split[0])
			if err != nil {
				return err
			}
			t.ToTime, err = time.Parse(STD_TIME_FORMAT_PERIOD, split[1])
			if err != nil {
				return err
			}
		} else {
			t.FromTime, err = time.Parse(STD_TIME_FORMAT_INSTANT, split[0])
			if err != nil {
				return err
			}
		}
	}
	return err
}
Ejemplo n.º 3
0
// SetBSON is a workaround for bson.Marshal not allowing
// for pointers to structs with ,inline option.
// It fails with:
//
//   Option ,inline needs a struct value or map field
//
// It should eventually be fixed and workaround dropped.
func (in *Inliner) SetBSON(raw bson.Raw) error {
	if err := raw.Unmarshal(in.SecondAddr()); err != nil {
		return err
	}

	return raw.Unmarshal(in.FirstAddr())
}
Ejemplo n.º 4
0
func (s *S) TestUnmarshalRawNil(c *C) {
	// Regression test: shouldn't try to nil out the pointer itself,
	// as it's not settable.
	raw := bson.Raw{0x0A, []byte{}}
	err := raw.Unmarshal(&struct{}{})
	c.Assert(err, IsNil)
}
Ejemplo n.º 5
0
// SetBSON allows us to use dependency representation of both
// just task Ids and of true Dependency structs.
//  TODO eventually drop all of this switching
func (d *Dependency) SetBSON(raw bson.Raw) error {
	// copy the Dependency type to remove this SetBSON method but preserve bson struct tags
	type nakedDep Dependency
	var depCopy nakedDep
	if err := raw.Unmarshal(&depCopy); err == nil {
		if depCopy.TaskId != "" {
			*d = Dependency(depCopy)
			return nil
		}
	}

	// hack to support the legacy depends_on, since we can't just unmarshal a string
	strBytes, _ := bson.Marshal(bson.RawD{{"str", raw}})
	var strStruct struct {
		String string `bson:"str"`
	}
	if err := bson.Unmarshal(strBytes, &strStruct); err == nil {
		if strStruct.String != "" {
			d.TaskId = strStruct.String
			d.Status = evergreen.TaskSucceeded
			return nil
		}
	}

	return bson.SetZero
}
Ejemplo n.º 6
0
// SetBSON implements bson.Setter
func (l *Level) SetBSON(raw bson.Raw) (err error) {
	var t bsonLevel
	err = raw.Unmarshal(&t)
	if err != nil {
		return err
	}
	m := map[Level]bool{
		Level(0400): t.UserCanRead,
		Level(0200): t.UserCanWrite,
		Level(0100): t.UserCanExecute,
		Level(0040): t.GroupCanWrite,
		Level(0020): t.GroupCanRead,
		Level(0010): t.GroupCanExecute,
		Level(0004): t.OtherCanRead,
		Level(0002): t.OtherCanWrite,
		Level(0001): t.OtherCanExecute,
	}
	for p, b := range m {
		if !b {
			continue
		}
		*l = *l | p
	}
	return
}
Ejemplo n.º 7
0
func (bd *BSONDump) Debug() error {
	stream, err := bd.init()
	if err != nil {
		return err
	}

	defer stream.Close()

	reusableBuf := make([]byte, db.MaxBSONSize)
	var result bson.Raw
	for {
		hasDoc, docSize := stream.LoadNextInto(reusableBuf)
		if !hasDoc {
			break
		}
		result.Kind = reusableBuf[0]
		result.Data = reusableBuf[0:docSize]
		err = DebugBSON(result, 0, os.Stdout)
		if err != nil {
			return err
		}
	}
	if err := stream.Err(); err != nil {
		return err
	}
	return nil
}
Ejemplo n.º 8
0
func (s *getterSetterD) SetBSON(raw bson.Raw) error {
	var doc bson.D
	err := raw.Unmarshal(&doc)
	doc = append(doc, bson.DocElem{"suffix", true})
	*s = getterSetterD(doc)
	return err
}
Ejemplo n.º 9
0
Archivo: event.go Proyecto: tsuru/tsuru
func (id *eventID) SetBSON(raw bson.Raw) error {
	err := raw.Unmarshal(&id.Target)
	if err != nil {
		return raw.Unmarshal(&id.ObjId)
	}
	return nil
}
Ejemplo n.º 10
0
func (d *DateTime) SetBSON(raw bson.Raw) error {
	var tm time.Time
	if err := raw.Unmarshal(&tm); err != nil {
		return err
	}
	*d = DateTime(tm)
	return nil
}
Ejemplo n.º 11
0
func (s *ifaceSlice) SetBSON(raw bson.Raw) error {
	var ns []int
	if err := raw.Unmarshal(&ns); err != nil {
		return err
	}
	*s = make(ifaceSlice, ns[0])
	return nil
}
Ejemplo n.º 12
0
func (m *settingsMap) SetBSON(raw bson.Raw) error {
	rawMap := make(map[string]interface{})
	if err := raw.Unmarshal(rawMap); err != nil {
		return err
	}
	replaceKeys(rawMap, unescapeReplacer.Replace)
	*m = settingsMap(rawMap)
	return nil
}
func (t *RFC2822Time) SetBSON(raw bson.Raw) error {
	var result time.Time
	err := raw.Unmarshal(&result)
	if err != nil {
		return err
	}
	*t = RFC2822Time(result)
	return nil
}
Ejemplo n.º 14
0
func (s *S) TestUnmarshalRawStructItems(c *C) {
	for i, item := range structItems {
		raw := bson.Raw{0x03, []byte(wrapInDoc(item.data))}
		zero := makeZeroDoc(item.obj)
		err := raw.Unmarshal(zero)
		c.Assert(err, IsNil)
		c.Assert(zero, DeepEquals, item.obj, Commentf("Failed on item %d: %#v", i, item))
	}
}
Ejemplo n.º 15
0
func (t *Timestamp) SetBSON(raw bson.Raw) error {
	var s string
	err := raw.Unmarshal(&s)
	if err != nil {
		return err
	}
	*t, err = ParseTimestamp(s)
	return err
}
Ejemplo n.º 16
0
func (d *DurationString) SetBSON(raw bson.Raw) error {
	var s string
	err := raw.Unmarshal(&s)
	if err != nil || s == "" {
		d.Duration = 0
		return err
	}
	d.Duration, err = time.ParseDuration(s)
	return err
}
Ejemplo n.º 17
0
func (l *List) SetBSON(raw bson.Raw) error {
	var m struct {
		Sample *Node `bson:"sample"`
	}
	if err := raw.Unmarshal(&m); err != nil {
		return err
	}
	l.sample = m.Sample
	return nil
}
Ejemplo n.º 18
0
Archivo: uuid.go Proyecto: mdigger/uuid
// SetBSON deserializes the UUID from the internal binary representation of JSON.
func (uuid *UUID) SetBSON(raw bson.Raw) error {
	var bin = new(bson.Binary)
	if err := raw.Unmarshal(bin); err != nil {
		return err
	}
	if bin.Kind != 0x04 {
		return errors.New("bson: bad UUID binary type")
	}
	return uuid.UnmarshalBinary(bin.Data)
}
Ejemplo n.º 19
0
func (e *EncryptedString) SetBSON(raw bson.Raw) error {
	var str string
	raw.Unmarshal(&str)
	decrypted, err := util.Decrypt(key, str)
	if err != nil {
		return err
	}

	*e = EncryptedString(decrypted)
	return nil
}
Ejemplo n.º 20
0
// SetBSON implements bson.Setter since the bson library does not look at underlying types and matches directly the time.Time type
func (dt *DateTime) SetBSON(raw bson.Raw) error {
	decoded := time.Time{}

	bsonErr := raw.Unmarshal(&decoded)
	if bsonErr != nil {
		return bsonErr
	}
	*dt = DateTime(decoded)
	return nil

}
Ejemplo n.º 21
0
func (o *setterType) SetBSON(raw bson.Raw) error {
	err := raw.Unmarshal(&o.received)
	if err != nil {
		panic("The panic:" + err.Error())
	}
	if s, ok := o.received.(string); ok {
		if result, ok := setterResult[s]; ok {
			return result
		}
	}
	return nil
}
Ejemplo n.º 22
0
func (t *TimeInstant) SetBSON(raw bson.Raw) error {
	var str string
	err := raw.Unmarshal(&str)
	if err == nil {
		tv, err := time.Parse(STD_TIME_FORMAT_INSTANT, strings.Replace(str, "\"", "", -1))
		if err != nil {
			return err
		}
		*t = TimeInstant(tv)
	}
	return err
}
Ejemplo n.º 23
0
func (k *Keywords) SetBSON(raw bson.Raw) error {
	var v []Keyword
	if err := raw.Unmarshal(&v); err != nil {
		return err
	}
	if v == nil || len(v) == 0 {
		return nil
	}

	*k = NewKeywords(v...)
	return nil
}
Ejemplo n.º 24
0
// SetBSON turns v into a bson.Setter so it can be loaded directly
// from a MongoDB database with mgo.
func (vp *Binary) SetBSON(raw bson.Raw) error {
	var s string
	err := raw.Unmarshal(&s)
	if err != nil {
		return err
	}
	v, err := ParseBinary(s)
	if err != nil {
		return err
	}
	*vp = v
	return nil
}
Ejemplo n.º 25
0
// SetBSON implements bson.Setter.
func (n *Number) SetBSON(raw bson.Raw) error {
	var s string
	err := raw.Unmarshal(&s)
	if err != nil {
		return err
	}
	v, err := Parse(s)
	if err != nil {
		return err
	}
	*n = v
	return nil
}
Ejemplo n.º 26
0
func (s *Schema) SetBSON(raw bson.Raw) error {
	var a struct {
		Id   bson.ObjectId `bson:"_id"`
		Name string        `bson:"name"`
		Root *Node         `bson:"root"`
	}
	if err := raw.Unmarshal(&a); err != nil {
		return err
	}
	*s = Schema(a)
	s.Root.setup(a.Name)
	return nil
}
Ejemplo n.º 27
0
func (o *Bool) SetBSON(raw bson.Raw) error {
	if o.readonly {
		return nil
	}
	var v *bool
	if err := raw.Unmarshal(&v); err != nil {
		return err
	}
	if v != nil {
		o.value = *v
		o.ok = true
	}
	return nil
}
Ejemplo n.º 28
0
// SetBSON ensures any special characters ($ or .) are unescaped in keys after
// unmarshalling the raw BSON coming from the stored document.
func (bp *bindingsMap) SetBSON(raw bson.Raw) error {
	rawMap := make(map[string]string)
	if err := raw.Unmarshal(rawMap); err != nil {
		return err
	}
	for key, value := range rawMap {
		newKey := unescapeReplacer.Replace(key)
		if newKey != key {
			delete(rawMap, key)
		}
		rawMap[newKey] = value
	}
	*bp = bindingsMap(rawMap)
	return nil
}
Ejemplo n.º 29
0
func (n *ScopedConfig) unmarshal(raw *bson.Raw, dst interface{}) error {
	if !n.Jsonfy {
		return raw.Unmarshal(dst)
	}
	var mapresult map[string]interface{}
	err := raw.Unmarshal(&mapresult)
	if err != nil {
		return err
	}
	data, err := json.Marshal(mapresult)
	if err != nil {
		return err
	}
	return json.Unmarshal(data, dst)
}
Ejemplo n.º 30
0
// SetBSON updates the internal members with the data stored in the bson.Raw
// parameter.
func (t *Tools) SetBSON(raw bson.Raw) error {
	if raw.Kind == 10 {
		// Preserve the nil value in that case.
		return bson.SetZero
	}
	var doc toolsDoc
	if err := raw.Unmarshal(&doc); err != nil {
		return err
	}
	t.Version = doc.Version
	t.URL = doc.URL
	t.Size = doc.Size
	t.SHA256 = doc.SHA256
	return nil
}