@@ -8,12 +8,12 @@ import qualified Common.Common as Pb
88
99import Control.Monad.Except ( ExceptT (.. ), runExceptT , throwError )
1010
11- -- import Data.Int (fromIntegral)
1211import Data.Bifunctor
1312import Data.ByteString as BS
1413import qualified Data.ByteString.Lazy as LBS
14+ import Data.Char ( chr )
1515import Data.IORef ( modifyIORef , newIORef , readIORef , writeIORef )
16- import Data.Text
16+ import Data.Text as TS
1717import Data.Text.Encoding
1818import Data.Text.Lazy as TL
1919import Data.Vector as Vector ( (!) , Vector , empty , foldr , length , toList )
@@ -167,26 +167,34 @@ instance ChaincodeStubInterface DefaultChaincodeStub where
167167 runExceptT $ ExceptT (listenForResponse (recvStream ccs)) >>= (bsToSqiAndMeta ccs)
168168
169169 -- TODO: This is the next TODO! Implement these 7 function because they are needed in marbles.hs
170- -- getStateByPartialCompositeKey :: ccs -> String -> [String ] -> Either Error StateQueryIterator
170+ -- getStateByPartialCompositeKey :: ccs -> Text -> [Text ] -> Either Error StateQueryIterator
171171 getStateByPartialCompositeKey ccs objectType keys = throwError $ Error " not implemented"
172172
173- -- getStateByPartialCompositeKeyWithPagination :: ccs -> String -> [String ] -> Int32 -> String -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
173+ -- getStateByPartialCompositeKeyWithPagination :: ccs -> Text -> [Text ] -> Int32 -> Text -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
174174 getStateByPartialCompositeKeyWithPagination ccs objectType keys pageSize bookmark =
175175 throwError $ Error " not implemented"
176176
177- -- createCompositeKey :: ccs -> String -> [String] -> Either Error String
178- createCompositeKey ccs objectType keys = throwError $ Error " not implemented"
177+ -- createCompositeKey :: ccs -> Text -> [Text] -> Either Error Text
178+ createCompositeKey ccs objectType keys =
179+ let keysString = Prelude. foldr (\ key acc -> acc ++ TS. unpack key ++ nullCodepoint) " " keys
180+ nullCodepoint = [ chr 0 ]
181+ in
182+ -- TODO: Check that objectTypes and keys are all valid utf8 strings
183+ Right $ TS. pack $ " \x00 " ++ TS. unpack objectType ++ nullCodepoint ++ keysString
179184
180- -- splitCompositeKey :: ccs -> String -> Either Error (String, [String])
181- splitCompositeKey ccs key = throwError $ Error " not implemented"
185+ -- splitCompositeKey :: ccs -> Text -> Either Error (Text, [Text])
186+ splitCompositeKey ccs key =
187+ -- key has the form \x00objectTypeU+0000keyU+0000key etc so we use `tail key` to ignore the \x00 char
188+ -- and then split on the unicode codepoint U+0000 to extract the objectType and keys
189+ let keys = TS. splitOn (TS. singleton $ chr 0 ) (TS. tail key) in Right (Prelude. head keys, Prelude. tail keys)
182190
183- -- getQueryResult :: ccs -> String -> Either Error StateQueryIterator
191+ -- getQueryResult :: ccs -> Text -> Either Error StateQueryIterator
184192 getQueryResult ccs query = throwError $ Error " not implemented"
185193
186- -- getQueryResultWithPagination :: ccs -> String -> Int32 -> String -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
194+ -- getQueryResultWithPagination :: ccs -> Text -> Int32 -> Text -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
187195 getQueryResultWithPagination ccs key pageSize bookmark = throwError $ Error " not implemented"
188196
189- -- getHistoryForKey :: ccs -> String -> Either Error HistoryQueryIterator
197+ -- getHistoryForKey :: ccs -> Text -> Either Error HistoryQueryIterator
190198 getHistoryForKey ccs key = throwError $ Error " not implemented"
191199
192200instance StateQueryIteratorInterface StateQueryIterator where
0 commit comments