Пример #1
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
}
Пример #2
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
}
Пример #3
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
}
Пример #4
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
}
Пример #5
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)
}
Пример #6
0
Файл: bson.go Проект: 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))
}
Пример #7
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())
}
Пример #8
0
func (id *eventID) SetBSON(raw bson.Raw) error {
	err := raw.Unmarshal(&id.Target)
	if err != nil {
		return raw.Unmarshal(&id.ObjId)
	}
	return nil
}
Пример #9
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
}
Пример #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
}
Пример #11
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
}
Пример #13
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
}
Пример #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))
	}
}
Пример #15
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
}
Пример #16
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
}
Пример #17
0
// 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)
}
Пример #18
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

}
Пример #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
}
Пример #20
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
}
Пример #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
}
Пример #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
}
Пример #23
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
}
Пример #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
}
Пример #25
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
}
Пример #26
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
}
Пример #27
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
}
Пример #28
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)
}
Пример #29
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
}
Пример #30
0
func (b *RedisEventBus) receiveGlobal(ready chan struct{}) {
	for {
		switch n := b.conn.Receive().(type) {
		case redis.PMessage:
			// Extract the event type from the channel name.
			eventType := strings.TrimPrefix(n.Channel, b.prefix)

			// Get the registered factory function for creating events.
			f, ok := b.factories[eventType]
			if !ok {
				log.Printf("error: event bus receive: %v\n", ErrEventNotRegistered)
				continue
			}

			// Manually decode the raw BSON event.
			data := bson.Raw{3, n.Data}
			event := f()
			if err := data.Unmarshal(event); err != nil {
				log.Printf("error: event bus receive: %v\n", ErrCouldNotUnmarshalEvent)
				continue
			}

			if handlers, ok := b.eventHandlers[event.EventType()]; ok {
				for handler := range handlers {
					handler.HandleEvent(event)
				}
			}

			for handler := range b.globalHandlers {
				handler.HandleEvent(event)
			}
		case redis.Subscription:
			switch n.Kind {
			case "psubscribe":
				close(ready)
			case "punsubscribe":
				if n.Count == 0 {
					close(b.exit)
					return
				}
			}
		case error:
			log.Printf("error: event bus receive: %v\n", n)
			close(b.exit)
			return
		}
	}
}