func (db *DB) load() error { data, err := util.LoadMap(db.filePath) if err != nil { db.data = map[string]interface{}{} return err } db.data = data return nil }
func setupEditor(server *Server) { manager := schema.GetManager() config := util.GetConfig() editableSchemaFile := config.GetString("editable_schema", "") golang.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 := 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, _ := 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.LoadMap(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") } newSchema := context["resource"].(map[string]interface{}) _, err := schema.NewSchemaFromObj(newSchema) if err != nil { return err } 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 } _, err := schema.NewSchemaFromObj(existingSchema) if err != nil { return err } 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 }) }
//LoadSchemaFromFile loads schema from json file func (manager *Manager) LoadSchemaFromFile(filePath string) error { log.Info("Loading schema %s ...", filePath) schemas, err := util.LoadMap(filePath) if err != nil { return err } if !(strings.HasPrefix(filePath, "http://") || strings.HasPrefix(filePath, "https://") || strings.HasPrefix(filePath, "embed://")) { 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) } _, err = reorderSchemas(schemaObjList) if err != nil { log.Warning("Error in reordering schema %s", err) } for _, schemaObj := range schemaObjList { if schemaObj.IsAbstract() { // Register abstract schema manager.RegisterSchema(schemaObj) } else { for _, baseSchemaID := range schemaObj.Extends { baseSchema, ok := manager.Schema(baseSchemaID) if !ok { return fmt.Errorf("Base Schema %s not found", baseSchemaID) } if !baseSchema.IsAbstract() { return fmt.Errorf("Base Schema %s isn't abstract type", baseSchemaID) } schemaObj.Extend(baseSchema) } } } // Reorder schema by relation topology schemaOrder, err := reorderSchemas(schemaObjList) if err != nil { log.Warning("Error in reordering schema %s", err) } for _, id := range schemaOrder { schemaObj := schemaMap[id] if !schemaObj.IsAbstract() { 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 } extension.File = filePath manager.Extensions = append(manager.Extensions, extension) } } return nil }