From 7fbe95de300c7cf77078cc00301f75afe3615e40 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Fri, 10 Sep 2021 16:14:12 -0400 Subject: [PATCH] Use enum filter to respond with proper enum model --- app/config/collections2.php | 2 +- app/controllers/api/database.php | 1 + app/init.php | 16 ++++++++++++++++ src/Appwrite/Utopia/Response.php | 13 ++++++++----- .../Utopia/Response/Model/AttributeList.php | 1 + .../Utopia/Response/Model/Collection.php | 1 + 6 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/config/collections2.php b/app/config/collections2.php index 6e13f19f2..e4f5c0147 100644 --- a/app/config/collections2.php +++ b/app/config/collections2.php @@ -214,7 +214,7 @@ $collections = [ 'required' => false, 'default' => new stdClass, 'array' => false, - 'filters' => ['json', 'range'], + 'filters' => ['json', 'range', 'enum'], ], [ '$id' => 'filters', diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 78df069ed..6c284e207 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -900,6 +900,7 @@ App::get('/v1/database/collections/:collectionId/attributes/:attributeId') Database::VAR_FLOAT => Response::MODEL_ATTRIBUTE_FLOAT, Database::VAR_STRING => match($format) { APP_DATABASE_ATTRIBUTE_EMAIL => Response::MODEL_ATTRIBUTE_EMAIL, + APP_DATABASE_ATTRIBUTE_ENUM => Response::MODEL_ATTRIBUTE_ENUM, APP_DATABASE_ATTRIBUTE_IP => Response::MODEL_ATTRIBUTE_IP, APP_DATABASE_ATTRIBUTE_URL => Response::MODEL_ATTRIBUTE_URL, default => Response::MODEL_ATTRIBUTE_STRING, diff --git a/app/init.php b/app/init.php index d1cc3fda1..54a6428cd 100644 --- a/app/init.php +++ b/app/init.php @@ -198,6 +198,22 @@ Database::addFilter('casting', } ); +Database::addFilter('enum', + function($value, Document $attribute) { + if ($attribute->isSet('elements')) { + $attribute->removeAttribute('elements'); + } + return $value; + }, + function($value, Document $attribute) { + $formatOptions = json_decode($attribute->getAttribute('formatOptions', []), true); + if (isset($formatOptions['elements'])) { + $attribute->setAttribute('elements', $formatOptions['elements']); + } + return $value; + } +); + Database::addFilter('range', function($value, Document $attribute) { if ($attribute->isSet('min')) { diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index a653ddbb0..6a1ff2307 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -17,6 +17,7 @@ use Appwrite\Utopia\Response\Model\AttributeInteger; use Appwrite\Utopia\Response\Model\AttributeFloat; use Appwrite\Utopia\Response\Model\AttributeBoolean; use Appwrite\Utopia\Response\Model\AttributeEmail; +use Appwrite\Utopia\Response\Model\AttributeEnum; use Appwrite\Utopia\Response\Model\AttributeIP; use Appwrite\Utopia\Response\Model\AttributeURL; use Appwrite\Utopia\Response\Model\BaseList; @@ -79,11 +80,12 @@ class Response extends SwooleResponse const MODEL_ATTRIBUTE = 'attribute'; const MODEL_ATTRIBUTE_LIST = 'attributeList'; const MODEL_ATTRIBUTE_STRING = 'attributeString'; - const MODEL_ATTRIBUTE_INTEGER= 'attributeInteger'; - const MODEL_ATTRIBUTE_FLOAT= 'attributeFloat'; - const MODEL_ATTRIBUTE_BOOLEAN= 'attributeBoolean'; - const MODEL_ATTRIBUTE_EMAIL= 'attributeEmail'; - const MODEL_ATTRIBUTE_IP= 'attributeIp'; + const MODEL_ATTRIBUTE_INTEGER = 'attributeInteger'; + const MODEL_ATTRIBUTE_FLOAT = 'attributeFloat'; + const MODEL_ATTRIBUTE_BOOLEAN = 'attributeBoolean'; + const MODEL_ATTRIBUTE_EMAIL = 'attributeEmail'; + const MODEL_ATTRIBUTE_ENUM = 'attributeEnum'; + const MODEL_ATTRIBUTE_IP = 'attributeIp'; const MODEL_ATTRIBUTE_URL= 'attributeUrl'; // Users @@ -201,6 +203,7 @@ class Response extends SwooleResponse ->setModel(new AttributeFloat()) ->setModel(new AttributeBoolean()) ->setModel(new AttributeEmail()) + ->setModel(new AttributeEnum()) ->setModel(new AttributeIP()) ->setModel(new AttributeURL()) ->setModel(new Index()) diff --git a/src/Appwrite/Utopia/Response/Model/AttributeList.php b/src/Appwrite/Utopia/Response/Model/AttributeList.php index 26ea146ec..69e083ced 100644 --- a/src/Appwrite/Utopia/Response/Model/AttributeList.php +++ b/src/Appwrite/Utopia/Response/Model/AttributeList.php @@ -29,6 +29,7 @@ class AttributeList extends Model self::TYPE_FLOAT => Response::MODEL_ATTRIBUTE_FLOAT, self::TYPE_STRING => match($attribute->getAttribute('format')) { APP_DATABASE_ATTRIBUTE_EMAIL => Response::MODEL_ATTRIBUTE_EMAIL, + APP_DATABASE_ATTRIBUTE_ENUM => Response::MODEL_ATTRIBUTE_ENUM, APP_DATABASE_ATTRIBUTE_IP => Response::MODEL_ATTRIBUTE_IP, APP_DATABASE_ATTRIBUTE_URL => Response::MODEL_ATTRIBUTE_URL, default => Response::MODEL_ATTRIBUTE_STRING, diff --git a/src/Appwrite/Utopia/Response/Model/Collection.php b/src/Appwrite/Utopia/Response/Model/Collection.php index f023be261..f59674bbf 100644 --- a/src/Appwrite/Utopia/Response/Model/Collection.php +++ b/src/Appwrite/Utopia/Response/Model/Collection.php @@ -57,6 +57,7 @@ class Collection extends Model self::TYPE_FLOAT => Response::MODEL_ATTRIBUTE_FLOAT, self::TYPE_STRING => match($attribute->getAttribute('format')) { APP_DATABASE_ATTRIBUTE_EMAIL => Response::MODEL_ATTRIBUTE_EMAIL, + APP_DATABASE_ATTRIBUTE_ENUM => Response::MODEL_ATTRIBUTE_ENUM, APP_DATABASE_ATTRIBUTE_IP => Response::MODEL_ATTRIBUTE_IP, APP_DATABASE_ATTRIBUTE_URL => Response::MODEL_ATTRIBUTE_URL, default => Response::MODEL_ATTRIBUTE_STRING,