Exemplo n.º 1
0
Arquivo: action.go Projeto: bac/juju
func importActionV1(source map[string]interface{}) (*action, error) {
	fields := schema.Fields{
		"receiver":   schema.String(),
		"name":       schema.String(),
		"parameters": schema.StringMap(schema.Any()),
		"enqueued":   schema.Time(),
		"started":    schema.Time(),
		"completed":  schema.Time(),
		"status":     schema.String(),
		"message":    schema.String(),
		"results":    schema.StringMap(schema.Any()),
		"id":         schema.String(),
	}
	// Some values don't have to be there.
	defaults := schema.Defaults{
		"started":   time.Time{},
		"completed": time.Time{},
	}
	checker := schema.FieldMap(fields, defaults)

	coerced, err := checker.Coerce(source, nil)
	if err != nil {
		return nil, errors.Annotatef(err, "action v1 schema check failed")
	}
	valid := coerced.(map[string]interface{})
	action := &action{
		Id_:         valid["id"].(string),
		Receiver_:   valid["receiver"].(string),
		Name_:       valid["name"].(string),
		Status_:     valid["status"].(string),
		Message_:    valid["message"].(string),
		Parameters_: valid["parameters"].(map[string]interface{}),
		Enqueued_:   valid["enqueued"].(time.Time).UTC(),
		Results_:    valid["results"].(map[string]interface{}),
	}

	started := valid["started"].(time.Time)
	if !started.IsZero() {
		started = started.UTC()
		action.Started_ = &started
	}
	completed := valid["completed"].(time.Time)
	if !started.IsZero() {
		completed = completed.UTC()
		action.Completed_ = &completed
	}
	return action, nil
}
Exemplo n.º 2
0
func importStatusV1(source map[string]interface{}) (StatusPoint_, error) {
	fields := schema.Fields{
		"value":   schema.String(),
		"message": schema.String(),
		"data":    schema.StringMap(schema.Any()),
		"updated": schema.Time(),
	}
	// Some values don't have to be there.
	defaults := schema.Defaults{
		"message": "",
		"data":    schema.Omit,
	}
	checker := schema.FieldMap(fields, defaults)

	coerced, err := checker.Coerce(source, nil)
	if err != nil {
		return StatusPoint_{}, errors.Annotatef(err, "status v1 schema check failed")
	}
	valid := coerced.(map[string]interface{})
	// From here we know that the map returned from the schema coercion
	// contains fields of the right type.

	var data map[string]interface{}
	if sourceData, set := valid["data"]; set {
		data = sourceData.(map[string]interface{})
	}
	return StatusPoint_{
		Value_:   valid["value"].(string),
		Message_: valid["message"].(string),
		Data_:    data,
		Updated_: valid["updated"].(time.Time),
	}, nil
}
Exemplo n.º 3
0
Arquivo: user.go Projeto: bac/juju
func importUserV1(source map[string]interface{}) (*user, error) {
	fields := schema.Fields{
		"name":            schema.String(),
		"display-name":    schema.String(),
		"created-by":      schema.String(),
		"read-only":       schema.Bool(),
		"date-created":    schema.Time(),
		"last-connection": schema.Time(),
		"access":          schema.String(),
	}

	// Some values don't have to be there.
	defaults := schema.Defaults{
		"display-name":    "",
		"last-connection": time.Time{},
		"read-only":       false,
	}
	checker := schema.FieldMap(fields, defaults)
	coerced, err := checker.Coerce(source, nil)
	if err != nil {
		return nil, errors.Annotatef(err, "user v1 schema check failed")
	}
	valid := coerced.(map[string]interface{})
	// From here we know that the map returned from the schema coercion
	// contains fields of the right type.

	result := &user{
		Name_:        valid["name"].(string),
		DisplayName_: valid["display-name"].(string),
		CreatedBy_:   valid["created-by"].(string),
		DateCreated_: valid["date-created"].(time.Time),
		Access_:      valid["access"].(string),
	}

	lastConn := valid["last-connection"].(time.Time)
	if !lastConn.IsZero() {
		result.LastConnection_ = &lastConn
	}

	return result, nil

}