From 5fc2a6ec3d6f6a017fd7bb9f3c8d52496f056d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Pit-Claudel?= <clement.pit-claudel@epfl.ch> Date: Sun, 22 Dec 2024 11:06:49 +0100 Subject: [PATCH] messages: Refactor ListAppsResponse.Wire --- .../main/scala/cs214/webapp/Messages.scala | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/shared/src/main/scala/cs214/webapp/Messages.scala b/shared/src/main/scala/cs214/webapp/Messages.scala index 7457ee2..46954f3 100644 --- a/shared/src/main/scala/cs214/webapp/Messages.scala +++ b/shared/src/main/scala/cs214/webapp/Messages.scala @@ -49,27 +49,24 @@ val SocketResponseWire = TryWire(IdentityWire) /** A response to the list-apps query */ case class ListAppsResponse(apps: Seq[AppInfo]) +object AppInfoWire extends WireFormat[AppInfo]: + def encode(t: AppInfo): ujson.Value = t match + case AppInfo(id, name, description, year) => + Obj("id" -> id, "name" -> name, "description" -> description, "year" -> year) + def decode(js: ujson.Value): Try[AppInfo] = Try: + AppInfo( + id = js("id").str, + name = js("name").str, + description = js("description").str, + year = js("year").num.toInt + ) + object ListAppsResponse: object Wire extends WireFormat[ListAppsResponse]: def encode(t: ListAppsResponse): ujson.Value = - ujson.Obj("apps" -> Arr(t.apps.map { - case AppInfo(id, name, description, year) => - Obj("id" -> id, "name" -> name, "desc" -> description, "year" -> year) - }*)) + Obj("apps" -> Arr(t.apps.map(AppInfoWire.encode)*)) def decode(js: ujson.Value): Try[ListAppsResponse] = Try: - ListAppsResponse( - js("apps").arr - .map(_.obj) - .map(js => - AppInfo( - id = js("id").str, - name = js("name").str, - description = js("desc").str, - year = js("year").num.toInt - ) - ) - .to(Seq) - ) + ListAppsResponse(apps = js("apps").arr.map(AppInfoWire.decode(_).get).to(Seq)) /** An HTTP request sent to create a new application instance for the server */ case class CreateInstanceRequest(appName: String, userIds: Seq[UserId]) -- GitLab