func()

in internal/objectstore/objectstore.go [45:85]


func (o *ObjectStore) Register(value reflect.Value, instanceID string) error {
	var err error
	if value, err = canonicalValue(value); err != nil {
		return err
	}
	ptr := value.Pointer()

	if existing, found := o.objectToID[ptr]; found {
		if existing == instanceID {
			return nil
		}
		return fmt.Errorf("attempting to register %s as %s, but it was already registered as %s", value, instanceID, existing)
	}

	aliases := findAliases(value)

	if existing, found := o.idToObject[instanceID]; found {
		if existing == value {
			return nil
		}
		// Value already exists (e.g: a constructor made a callback with "this"
		// passed as an argument). We make the current value an alias of the new
		// one.
		aliases = append(aliases, existing)
	}

	for _, alias := range aliases {
		ptr := alias.Pointer()
		if existing, found := o.objectToID[ptr]; found && existing != instanceID {
			return fmt.Errorf("value %s is embedded in %s which has ID %s, but was already assigned %s", alias.String(), value.String(), instanceID, existing)
		}
	}

	o.objectToID[ptr] = instanceID
	o.idToObject[instanceID] = value
	for _, alias := range aliases {
		o.objectToID[alias.Pointer()] = instanceID
	}

	return nil
}