Exemplo n.º 1
0
func ResourceToMap(obj interface{}, schemas *client.Schemas) (map[string]interface{}, error) {
	result := map[string]interface{}{}
	if obj == nil {
		return result, nil
	}

	resource := getResource(obj)
	if resource == nil {
		return result, errors.New("value is not a Resource")
	}

	objMap, err := toMap(obj)
	if err != nil {
		return result, err
	}

	schema, ok := schemas.CheckSchema(resource.Type)
	if !ok {
		logrus.Errorf("Attempting to Write an unknown type: %s", resource.Type)
		return result, nil
	}

	for k, v := range objMap {
		_, ok := schema.CheckField(k)
		if !ok {
			continue
		}
		result[k] = v
	}

	return result, nil
}
Exemplo n.º 2
0
func ResourceToMap(obj interface{}, schemas *client.Schemas) (map[string]interface{}, error) {
	result := map[string]interface{}{}
	if obj == nil {
		return result, nil
	}

	resource := getResource(obj)
	if resource == nil {
		return result, errors.New("value is not a Resource")
	}

	objMap, err := toMap(obj)
	if err != nil {
		return result, err
	}

	schema := schemas.Schema(resource.Type)
	for k, v := range objMap {
		_, ok := schema.CheckField(k)
		if !ok {
			continue
		}
		result[k] = v
	}

	return result, nil
}
Exemplo n.º 3
0
func SchemaHandler(schemas *client.Schemas) http.Handler {
	return ApiHandler(schemas, http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		apiContext := GetApiContext(r)

		schema := schemas.Schema(mux.Vars(r)["id"])

		populateSchema(apiContext, &schema)

		apiContext.Write(&schema)
	}))
}