Exemple #1
0
//LoadSchemaFromFile loads schema from json file
func (manager *Manager) LoadSchemaFromFile(filePath string) error {
	log.Info("Loading schema %s ...", filePath)
	schemas, err := util.LoadFile(filePath)
	if err != nil {
		return err
	}

	workingDirectory, err := os.Getwd()
	if err != nil {
		return err
	}
	err = os.Chdir(filepath.Dir(filePath))
	if err != nil {
		return err
	}
	defer os.Chdir(workingDirectory)

	namespaces, _ := schemas["namespaces"].([]interface{})
	for _, namespaceData := range namespaces {
		namespace, err := NewNamespace(namespaceData)
		if err != nil {
			return err
		}
		if err = manager.RegisterNamespace(namespace); err != nil {
			return err
		}
	}
	list, _ := schemas["schemas"].([]interface{})
	for _, schemaData := range list {
		schemaObj, err := NewSchemaFromObj(schemaData)
		if err != nil {
			return err
		}
		err = manager.RegisterSchema(schemaObj)
		if err != nil {
			return err
		}
	}
	policies, _ := schemas["policies"].([]interface{})
	if policies != nil {
		for _, policyData := range policies {
			policy, err := NewPolicy(policyData)
			if err != nil {
				return err
			}
			manager.policies = append(manager.policies, policy)
		}
	}
	extensions, _ := schemas["extensions"].([]interface{})
	if extensions != nil {
		for _, extensionData := range extensions {
			extension, err := NewExtension(extensionData)
			if err != nil {
				return err
			}
			manager.Extensions = append(manager.Extensions, extension)
		}
	}
	return nil
}
Exemple #2
0
func (db *DB) load() error {
	data, err := util.LoadFile(db.filePath)
	if err != nil {
		db.data = map[string]interface{}{}
		return err
	}
	db.data = data
	return nil
}
Exemple #3
0
func setupEditor(server *Server) {
	manager := schema.GetManager()
	config := util.GetConfig()
	editableSchemaFile := config.GetString("editable_schema", "")
	extension.RegisterGoCallback("handle_schema",
		func(event string, context map[string]interface{}) error {
			auth := context["auth"].(schema.Authorization)
			if event == "pre_list" {
				list := []interface{}{}
				total := 0
				for _, currentSchema := range manager.OrderedSchemas() {
					trimmedSchema, err := schema.GetSchema(currentSchema, auth)
					if err != nil {
						return err
					}
					if trimmedSchema != nil {
						s := trimmedSchema.Data()
						s["url"] = currentSchema.URL
						list = append(list, s)
						total = total + 1
					}
				}
				context["total"] = total
				context["response"] = map[string]interface{}{
					"schemas": list,
				}
				return nil
			} else if event == "pre_show" {
				ID := context["id"].(string)
				currentSchema, _ := manager.Schema(ID)
				object, _ := schema.GetSchema(currentSchema, auth)
				s := object.Data()
				s["url"] = currentSchema.URL
				context["response"] = map[string]interface{}{
					"schema": s,
				}
				return nil
			}
			if event != "pre_create" && event != "pre_update" && event != "pre_delete" {
				return nil
			}

			if editableSchemaFile == "" {
				return nil
			}

			ID := context["id"].(string)

			schemasInFile, err := util.LoadFile(editableSchemaFile)
			if err != nil {
				return nil
			}
			schemas := schemasInFile["schemas"].([]interface{})
			updatedSchemas := []interface{}{}
			var existingSchema map[string]interface{}
			for _, rawSchema := range schemas {
				s := rawSchema.(map[string]interface{})
				if s["id"] == ID {
					existingSchema = s
				} else {
					updatedSchemas = append(updatedSchemas, s)
				}
			}

			if event == "pre_create" {
				if existingSchema != nil {
					return fmt.Errorf("ID has been taken")
				}
				schemasInFile["schemas"] = append(updatedSchemas, context["resource"].(map[string]interface{}))
				context["response"] = map[string]interface{}{
					"schema": context["resource"],
				}
				context["exception"] = map[string]interface{}{
					"name":    "CustomException",
					"message": context["response"],
					"code":    201,
				}
			} else if event == "pre_update" {
				if existingSchema == nil {
					return fmt.Errorf("Not found or Update isn't supported for this schema")
				}
				for key, value := range context["resource"].(map[string]interface{}) {
					existingSchema[key] = value
				}
				schemasInFile["schemas"] = append(updatedSchemas, existingSchema)
				context["response"] = map[string]interface{}{
					"schema": context["resource"],
				}
				context["exception"] = map[string]interface{}{
					"name":    "CustomException",
					"message": context["response"],
					"code":    200,
				}

			} else if event == "pre_delete" {
				if existingSchema == nil {
					return fmt.Errorf("Not found or Delete isn't supported for this schema")
				}
				schemasInFile["schemas"] = updatedSchemas
				deletedSchema, ok := manager.Schema(ID)
				if ok {
					manager.UnRegisterSchema(deletedSchema)
				}
				context["exception"] = map[string]interface{}{
					"name":    "CustomException",
					"message": map[string]interface{}{"result": "deleted"},
					"code":    204,
				}
			}
			util.SaveFile(editableSchemaFile, schemasInFile)
			err = manager.LoadSchemaFromFile(editableSchemaFile)
			if err != nil {
				return err
			}
			server.initDB()
			server.resetRouter()
			server.mapRoutes()
			return nil
		})
}
Exemple #4
0
//LoadSchemaFromFile loads schema from json file
func (manager *Manager) LoadSchemaFromFile(filePath string) error {
	log.Info("Loading schema %s ...", filePath)
	schemas, err := util.LoadFile(filePath)
	if err != nil {
		return err
	}

	if !(strings.HasPrefix(filePath, "http://") || strings.HasPrefix(filePath, "https://")) {
		workingDirectory, err := os.Getwd()
		if err != nil {
			return err
		}
		err = os.Chdir(filepath.Dir(filePath))
		if err != nil {
			return err
		}
		defer os.Chdir(workingDirectory)
	}

	namespaces, _ := schemas["namespaces"].([]interface{})
	for _, namespaceData := range namespaces {
		namespace, err := NewNamespace(namespaceData)
		if err != nil {
			return err
		}
		if err = manager.RegisterNamespace(namespace); err != nil {
			return err
		}
	}

	schemaObjList := []*Schema{}
	schemaMap := map[string]*Schema{}
	list, _ := schemas["schemas"].([]interface{})
	for _, schemaData := range list {
		schemaObj, err := NewSchemaFromObj(schemaData)
		if err != nil {
			return err
		}
		schemaMap[schemaObj.ID] = schemaObj
		schemaObjList = append(schemaObjList, schemaObj)
	}

	schemaOrder, err := reorderSchemas(schemaObjList)
	if err == nil {
		schemaObjList = []*Schema{}
		for _, id := range schemaOrder {
			schemaObjList = append(schemaObjList, schemaMap[id])
		}
	} else {
		log.Warning("Error in reordering schema %s", err)
	}

	for _, schemaObj := range schemaObjList {
		err = manager.RegisterSchema(schemaObj)
		if err != nil {
			return err
		}
	}

	policies, _ := schemas["policies"].([]interface{})
	if policies != nil {
		for _, policyData := range policies {
			policy, err := NewPolicy(policyData)
			if err != nil {
				return err
			}
			manager.policies = append(manager.policies, policy)
		}
	}
	extensions, _ := schemas["extensions"].([]interface{})
	if extensions != nil {
		for _, extensionData := range extensions {
			extension, err := NewExtension(extensionData)
			if err != nil {
				return err
			}
			manager.Extensions = append(manager.Extensions, extension)
		}
	}
	return nil
}
Exemple #5
0
//FetchContent fetch contents from arbitrary path
func FetchContent(path string) (interface{}, error) {
	return util.LoadFile(path)
}