From e99b7dbf2cefea495a31053b93f59257d9edede3 Mon Sep 17 00:00:00 2001 From: Tristan Cacqueray Date: Sun, 26 Jun 2022 17:17:40 +0000 Subject: [PATCH] Add Robot instances --- src/Swarm/Game/Robot.hs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Swarm/Game/Robot.hs b/src/Swarm/Game/Robot.hs index dec791d9df..004152d6ad 100644 --- a/src/Swarm/Game/Robot.hs +++ b/src/Swarm/Game/Robot.hs @@ -1,3 +1,5 @@ +{-# LANGUAGE DeriveAnyClass #-} +{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE OverloadedStrings #-} @@ -7,6 +9,7 @@ {-# LANGUAGE TypeApplications #-} {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE ViewPatterns #-} +{-# OPTIONS_GHC -fno-warn-orphans #-} -- | -- Module : Swarm.Game.Robot @@ -71,6 +74,7 @@ module Swarm.Game.Robot ( ) where import Control.Lens hiding (contains) +import Data.Aeson (FromJSON, ToJSON) import Data.Hashable (hashWithSalt) import Data.Int (Int64) import Data.Maybe (isNothing) @@ -79,6 +83,7 @@ import qualified Data.Sequence as Seq import Data.Set (Set) import Data.Set.Lens (setOf) import Data.Text (Text) +import GHC.Generics (Generic) import Linear import System.Clock (TimeSpec) @@ -109,7 +114,7 @@ data RobotContext = RobotContext -- definitions. _defStore :: Store } - deriving (Show) + deriving (Show, Generic, FromJSON, ToJSON) makeLenses ''RobotContext @@ -122,7 +127,7 @@ data LogEntry = LogEntry , -- | The time at which the entry was created. _leTime :: Integer } - deriving (Show) + deriving (Show, Generic, FromJSON, ToJSON) makeLenses ''LogEntry @@ -150,8 +155,14 @@ data RobotR f = RobotR , _tickSteps :: Int , _robotCreatedAt :: TimeSpec } + deriving (Generic) deriving instance Show (f RID) => Show (RobotR f) +deriving instance FromJSON (f RID) => FromJSON (RobotR f) +deriving instance ToJSON (f RID) => ToJSON (RobotR f) + +deriving instance FromJSON TimeSpec +deriving instance ToJSON TimeSpec -- See https://byorgey.wordpress.com/2021/09/17/automatically-updated-cached-views-with-lens/ -- for the approach used here with lenses.