<commit_before>{-
Copyright 2016 SlamData, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-}

module SlamData.Workspace.Card.Port.VarMap
  ( VarMap
  , URLVarMap
  , VarMapValue(..)
  , renderVarMapValue
  , emptyVarMap
  , escapeIdentifier
  ) where

import SlamData.Prelude

import Data.Foldable as F
import Data.HugeNum as HN
import Data.Json.Extended as EJSON
import Data.Json.Extended.Signature.Render as EJR
import Data.String.Regex (test, replace) as Regex
import Data.String.Regex.Flags (ignoreCase, global) as Regex
import Data.String.Regex.Unsafe (unsafeRegex) as Regex
import Data.StrMap as SM

import Data.Argonaut ((.?))
import Data.Argonaut.Core (jsonSingletonObject)
import Data.Argonaut.Decode (class DecodeJson, decodeJson)
import Data.Argonaut.Encode (class EncodeJson, encodeJson)

import Matryoshka (Algebra, cata)

import Text.Markdown.SlamDown.Syntax.Value as SDV

import Test.StrongCheck.Gen as Gen
import Test.StrongCheck.Arbitrary as SC

data VarMapValue
  = Literal EJSON.EJson
  | SetLiteral (Array VarMapValue)
  | QueryExpr String -- TODO: syntax of SQL^2 queries

derive instance eqVarMapValue ∷ Eq VarMapValue

derive instance ordVarMapValue ∷ Ord VarMapValue

instance showVarMapValue ∷ Show VarMapValue where
  show = renderVarMapValue

instance encodeJsonVarMapValue ∷ EncodeJson VarMapValue where
  encodeJson v =
    case v of
      Literal ejson →
        jsonSingletonObject "literal" $
          EJSON.encodeEJson ejson
      SetLiteral as →
        jsonSingletonObject "set" $
          encodeJson as
      QueryExpr str →
        jsonSingletonObject "query" $
          encodeJson str

instance decodeJsonVarMapValue :: DecodeJson VarMapValue where
  decodeJson json = do
    obj <- decodeJson json
    decodeLiteral obj
      <|> decodeSetLiteral obj
      <|> decodeQueryExpr obj

    where
      decodeLiteral =
        (_ .? "literal")
          >=> EJSON.decodeEJson
          >>> map Literal

      decodeSetLiteral =
        (_ .? "set")
          >=> decodeJson
          >>> map SetLiteral

      decodeQueryExpr  =
        (_ .? "query")
          >>> map QueryExpr

renderVarMapValue
  ∷ VarMapValue
  → String
renderVarMapValue val =
  case val of
    Literal lit → EJSON.renderEJson lit
    SetLiteral as → "(" <> F.intercalate "," (renderVarMapValue <$> as) <> ")"
    QueryExpr str → str

displayVarMapValue
  ∷ VarMapValue
  → String
displayVarMapValue val =
  case val of
    Literal lit → displayEJson lit
    SetLiteral as → "(" <> F.intercalate ", " (displayVarMapValue <$> as) <> ")"
    QueryExpr str → str

displayEJsonF ∷ Algebra EJSON.EJsonF String
displayEJsonF =
  case _ of
    EJSON.Null → "null"
    EJSON.Boolean b → if b then "true" else "false"
    EJSON.Integer i → show i
    EJSON.Decimal a → HN.toString a
    EJSON.String str → str
    EJSON.Timestamp dt → EJR.renderTimestamp dt
    EJSON.Time t → EJR.renderTime t
    EJSON.Date d → EJR.renderDate d
    EJSON.Interval str → str
    EJSON.ObjectId str → str
    EJSON.Array ds → "[" <> commaSep ds <> "]"
    EJSON.Map (EJSON.EJsonMap ds) → "(" <> commaSep (map renderPair ds) <> ")"
  where
  commaSep ∷ Array String → String
  commaSep = F.intercalate ","
  renderPair ∷ Tuple String String → String
  renderPair (Tuple k v) = k <> ": " <> v

-- | A more readable, but forgetful renderer
displayEJson ∷ EJSON.EJson → String
displayEJson = cata displayEJsonF

instance valueVarMapValue ∷ SDV.Value VarMapValue where<commit_msg>Fix printing of EJSON Map values<commit_after>{-
Copyright 2016 SlamData, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-}

module SlamData.Workspace.Card.Port.VarMap
  ( VarMap
  , URLVarMap
  , VarMapValue(..)
  , renderVarMapValue
  , emptyVarMap
  , escapeIdentifier
  ) where

import SlamData.Prelude

import Data.Foldable as F
import Data.HugeNum as HN
import Data.Json.Extended as EJSON
import Data.Json.Extended.Signature.Render as EJR
import Data.String.Regex (test, replace) as Regex
import Data.String.Regex.Flags (ignoreCase, global) as Regex
import Data.String.Regex.Unsafe (unsafeRegex) as Regex
import Data.StrMap as SM

import Data.Argonaut ((.?))
import Data.Argonaut.Core (jsonSingletonObject)
import Data.Argonaut.Decode (class DecodeJson, decodeJson)
import Data.Argonaut.Encode (class EncodeJson, encodeJson)

import Matryoshka (Algebra, cata)

import Text.Markdown.SlamDown.Syntax.Value as SDV

import Test.StrongCheck.Gen as Gen
import Test.StrongCheck.Arbitrary as SC

data VarMapValue
  = Literal EJSON.EJson
  | SetLiteral (Array VarMapValue)
  | QueryExpr String -- TODO: syntax of SQL^2 queries

derive instance eqVarMapValue ∷ Eq VarMapValue

derive instance ordVarMapValue ∷ Ord VarMapValue

instance showVarMapValue ∷ Show VarMapValue where
  show = renderVarMapValue

instance encodeJsonVarMapValue ∷ EncodeJson VarMapValue where
  encodeJson v =
    case v of
      Literal ejson →
        jsonSingletonObject "literal" $
          EJSON.encodeEJson ejson
      SetLiteral as →
        jsonSingletonObject "set" $
          encodeJson as
      QueryExpr str →
        jsonSingletonObject "query" $
          encodeJson str

instance decodeJsonVarMapValue :: DecodeJson VarMapValue where
  decodeJson json = do
    obj <- decodeJson json
    decodeLiteral obj
      <|> decodeSetLiteral obj
      <|> decodeQueryExpr obj

    where
      decodeLiteral =
        (_ .? "literal")
          >=> EJSON.decodeEJson
          >>> map Literal

      decodeSetLiteral =
        (_ .? "set")
          >=> decodeJson
          >>> map SetLiteral

      decodeQueryExpr  =
        (_ .? "query")
          >>> map QueryExpr

renderVarMapValue
  ∷ VarMapValue
  → String
renderVarMapValue val =
  case val of
    Literal lit → EJSON.renderEJson lit
    SetLiteral as → "(" <> F.intercalate "," (renderVarMapValue <$> as) <> ")"
    QueryExpr str → str

displayVarMapValue
  ∷ VarMapValue
  → String
displayVarMapValue val =
  case val of
    Literal lit → displayEJson lit
    SetLiteral as → "(" <> F.intercalate ", " (displayVarMapValue <$> as) <> ")"
    QueryExpr str → str

displayEJsonF ∷ Algebra EJSON.EJsonF String
displayEJsonF =
  case _ of
    EJSON.Null → "null"
    EJSON.Boolean b → if b then "true" else "false"
    EJSON.Integer i → show i
    EJSON.Decimal a → HN.toString a
    EJSON.String str → str
    EJSON.Timestamp dt → EJR.renderTimestamp dt
    EJSON.Time t → EJR.renderTime t
    EJSON.Date d → EJR.renderDate d
    EJSON.Interval str → str
    EJSON.ObjectId str → str
    EJSON.Array ds → "[" <> commaSep ds <> "]"
    EJSON.Map (EJSON.EJsonMap ds) → "{ " <> commaSep (map renderPair ds) <> " }"
  where
  commaSep ∷ Array String → String
  commaSep = F.intercalate ","
  renderPair ∷ Tuple String String → String
  renderPair (Tuple k v) = k <> ": " <> v

-- | A more readable, but forgetful renderer
displayEJson ∷ EJSON.EJson → String
displayEJson = cata displayEJsonF

instance valueVarMapValue ∷ SDV.Value VarMapValue where
{'fraction_non_alphanumeric': 0.0822888973287758, 'fraction_numerical': 0.002278769464489176, 'mean_word_length': 3.261057173678533, 'pattern_counts': {'":': 2, '<': 33, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6097759', 'n_tokens_mistral': 2528, 'n_tokens_neox': 2395, 'n_words': 848}