@@ -46,8 +46,9 @@ import qualified Data.Aeson as A
46
46
import qualified Data.Aeson.Types as A
47
47
import Data.Either (fromRight )
48
48
import Data.Function ((&) )
49
- import Data.Kind (Constraint )
49
+ import Data.Kind (Constraint , Type )
50
50
import qualified Data.Map.Strict as Map
51
+ import Data.Proxy (Proxy (.. ))
51
52
import qualified Data.Text as T
52
53
import GHC.OverloadedLabels (IsLabel (.. ))
53
54
import GHC.TypeLits
@@ -59,18 +60,18 @@ data PropertyType
59
60
| TInteger
60
61
| TString
61
62
| TBoolean
62
- | TObject
63
- | TArray
64
- | TEnum
63
+ | TObject Type
64
+ | TArray Type
65
+ | TEnum Type
65
66
66
67
type family ToHsType (t :: PropertyType ) where
67
68
ToHsType 'TNumber = Double -- in js, there are no distinct types for integers and floating-point values
68
69
ToHsType 'TInteger = Int -- so here we use Double for Number, Int for Integer
69
70
ToHsType 'TString = T. Text
70
71
ToHsType 'TBoolean = Bool
71
- ToHsType 'TObject = A. Object
72
- ToHsType 'TArray = A. Array
73
- ToHsType 'TEnum = T. Text -- supports only text enum now
72
+ ToHsType ( 'TObject a ) = a
73
+ ToHsType ( 'TArray a ) = [ a ]
74
+ ToHsType ( 'TEnum a ) = a
74
75
75
76
-- ---------------------------------------------------------------------
76
77
@@ -100,9 +101,9 @@ data SPropertyKey (k :: PropertyKey) where
100
101
SInteger :: SPropertyKey ('PropertyKey s 'TInteger)
101
102
SString :: SPropertyKey ('PropertyKey s 'TString)
102
103
SBoolean :: SPropertyKey ('PropertyKey s 'TBoolean)
103
- SObject :: SPropertyKey ('PropertyKey s 'TObject)
104
- SArray :: SPropertyKey ('PropertyKey s 'TArray)
105
- SEnum :: SPropertyKey ('PropertyKey s 'TEnum)
104
+ SObject :: ( A. ToJSON a , A. FromJSON a ) => Proxy a -> SPropertyKey ('PropertyKey s ( 'TObject a ) )
105
+ SArray :: ( A. ToJSON a , A. FromJSON a ) => Proxy a -> SPropertyKey ('PropertyKey s ( 'TArray a ) )
106
+ SEnum :: ( A. ToJSON a , A. FromJSON a , Eq a , Show a ) => Proxy a -> SPropertyKey ('PropertyKey s ( 'TEnum a ) )
106
107
107
108
-- | Existential wrapper of 'SPropertyKey', with an extra 'MetaData'
108
109
data SomePropertyKeyWithMetaData
@@ -126,7 +127,7 @@ instance (KnownSymbol s', s ~ s') => IsLabel s (KeyNameProxy s') where
126
127
-- ---------------------------------------------------------------------
127
128
128
129
type family IsTEnum (t :: PropertyType ) :: Bool where
129
- IsTEnum 'TEnum = 'True
130
+ IsTEnum ( 'TEnum _ ) = 'True
130
131
IsTEnum _ = 'False
131
132
132
133
type family FindByKeyName (s :: Symbol ) (r :: [PropertyKey ]) :: PropertyType where
@@ -234,9 +235,9 @@ parseProperty kn k x = case k of
234
235
(SInteger , _) -> parseEither
235
236
(SString , _) -> parseEither
236
237
(SBoolean , _) -> parseEither
237
- (SObject , _) -> parseEither
238
- (SArray , _) -> parseEither
239
- (SEnum , EnumMetaData {.. }) ->
238
+ (SObject _ , _) -> parseEither
239
+ (SArray _ , _) -> parseEither
240
+ (SEnum _ , EnumMetaData {.. }) ->
240
241
A. parseEither
241
242
( \ o -> do
242
243
txt <- o A. .: keyName
@@ -245,7 +246,7 @@ parseProperty kn k x = case k of
245
246
else
246
247
fail $
247
248
" invalid enum member: "
248
- <> T. unpack txt
249
+ <> show txt
249
250
<> " . Expected one of "
250
251
<> show enumValues
251
252
)
@@ -311,44 +312,43 @@ defineBooleanProperty kn description defaultValue =
311
312
312
313
-- | Defines an object property
313
314
defineObjectProperty ::
314
- forall s r .
315
- (KnownSymbol s , NotElem s r ) =>
315
+ (KnownSymbol s , NotElem s r , A. ToJSON a , A. FromJSON a ) =>
316
316
KeyNameProxy s ->
317
317
-- | description
318
318
T. Text ->
319
319
-- | default value
320
- A. Object ->
320
+ a ->
321
321
Properties r ->
322
- Properties ('PropertyKey s 'TObject : r )
322
+ Properties ('PropertyKey s ( 'TObject a ) : r )
323
323
defineObjectProperty kn description defaultValue =
324
- insert kn SObject MetaData {.. }
324
+ insert kn ( SObject Proxy ) MetaData {.. }
325
325
326
326
-- | Defines an array property
327
327
defineArrayProperty ::
328
- (KnownSymbol s , NotElem s r ) =>
328
+ (KnownSymbol s , NotElem s r , A. ToJSON a , A. FromJSON a ) =>
329
329
KeyNameProxy s ->
330
330
-- | description
331
331
T. Text ->
332
332
-- | default value
333
- A. Array ->
333
+ [ a ] ->
334
334
Properties r ->
335
- Properties ('PropertyKey s 'TArray : r )
335
+ Properties ('PropertyKey s ( 'TArray a ) : r )
336
336
defineArrayProperty kn description defaultValue =
337
- insert kn SArray MetaData {.. }
337
+ insert kn ( SArray Proxy ) MetaData {.. }
338
338
339
339
-- | Defines an enum property
340
340
defineEnumProperty ::
341
- (KnownSymbol s , NotElem s r ) =>
341
+ (KnownSymbol s , NotElem s r , A. ToJSON a , A. FromJSON a , Eq a , Show a ) =>
342
342
KeyNameProxy s ->
343
343
-- | description
344
344
T. Text ->
345
345
-- | valid enum members with each of description
346
- [(T. Text , T. Text )] ->
347
- T. Text ->
346
+ [(a , T. Text )] ->
347
+ a ->
348
348
Properties r ->
349
- Properties ('PropertyKey s 'TEnum : r )
349
+ Properties ('PropertyKey s ( 'TEnum a ) : r )
350
350
defineEnumProperty kn description enums defaultValue =
351
- insert kn SEnum $ EnumMetaData defaultValue description (fst <$> enums) (snd <$> enums)
351
+ insert kn ( SEnum Proxy ) $ EnumMetaData defaultValue description (fst <$> enums) (snd <$> enums)
352
352
353
353
-- ---------------------------------------------------------------------
354
354
@@ -366,11 +366,11 @@ toDefaultJSON (Properties p) = [toEntry s v | (s, v) <- Map.toList p]
366
366
s A. .= defaultValue
367
367
(SomePropertyKeyWithMetaData SBoolean MetaData {.. }) ->
368
368
s A. .= defaultValue
369
- (SomePropertyKeyWithMetaData SObject MetaData {.. }) ->
369
+ (SomePropertyKeyWithMetaData ( SObject _) MetaData {.. }) ->
370
370
s A. .= defaultValue
371
- (SomePropertyKeyWithMetaData SArray MetaData {.. }) ->
371
+ (SomePropertyKeyWithMetaData ( SArray _) MetaData {.. }) ->
372
372
s A. .= defaultValue
373
- (SomePropertyKeyWithMetaData SEnum EnumMetaData {.. }) ->
373
+ (SomePropertyKeyWithMetaData ( SEnum _) EnumMetaData {.. }) ->
374
374
s A. .= defaultValue
375
375
376
376
-- | Converts a properties definition into kv pairs as vscode schema
@@ -408,21 +408,21 @@ toVSCodeExtensionSchema prefix (Properties p) =
408
408
" default" A. .= defaultValue,
409
409
" scope" A. .= A. String " resource"
410
410
]
411
- (SomePropertyKeyWithMetaData SObject MetaData {.. }) ->
411
+ (SomePropertyKeyWithMetaData ( SObject _) MetaData {.. }) ->
412
412
A. object
413
413
[ " type" A. .= A. String " object" ,
414
414
" markdownDescription" A. .= description,
415
415
" default" A. .= defaultValue,
416
416
" scope" A. .= A. String " resource"
417
417
]
418
- (SomePropertyKeyWithMetaData SArray MetaData {.. }) ->
418
+ (SomePropertyKeyWithMetaData ( SArray _) MetaData {.. }) ->
419
419
A. object
420
420
[ " type" A. .= A. String " array" ,
421
421
" markdownDescription" A. .= description,
422
422
" default" A. .= defaultValue,
423
423
" scope" A. .= A. String " resource"
424
424
]
425
- (SomePropertyKeyWithMetaData SEnum EnumMetaData {.. }) ->
425
+ (SomePropertyKeyWithMetaData ( SEnum _) EnumMetaData {.. }) ->
426
426
A. object
427
427
[ " type" A. .= A. String " string" ,
428
428
" description" A. .= description,
0 commit comments