Skip to content

Commit

Permalink
fix(aeria-compiler): fixes "required" codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielolivrp committed Aug 9, 2024
1 parent c509a06 commit 8f8b8a7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
10 changes: 8 additions & 2 deletions aeria-compiler/src/Aeria/Codegen/Collection.purs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ cDescription (Collection
indexesDescription = listProperty "indexes" cNames indexes
presetsDescription = listProperty "presets" cPresets presets
writableDescription = listProperty "writable" cNames writable
requiredDescription = listProperty "required" cRequired required
requiredDescription = optionalProperty "required" cRequired required
tableMetaDescription = listProperty "tableMeta" cNames tableMeta
filtersPresetsDescription = listProperty "filtersPresets" cFiltersPresets filtersPresets
preferredDescription = listProperty "preferred" cPreferred preferred
Expand Down Expand Up @@ -465,7 +465,7 @@ cProperties properties = L.toUnfoldable (map go properties)
PObject _ required properties' additionalProperties ->
catMaybes
[ Just (cType "object")
, listProperty "required" cRequired required
, optionalProperty "required" cRequired required
, optionalProperty "properties" (Js.object <<< cProperties) (Just properties')
, optionalProperty "additionalProperties" cAdditionalProperties additionalProperties
]
Expand Down Expand Up @@ -565,6 +565,12 @@ cNames xs =
# L.toUnfoldable
# Js.array

listProperty' :: forall a. String -> (L.List a -> Js.Tree) -> L.List a -> Maybe Js.ObjectProperty
listProperty' k f x =
case x of
L.Nil -> Just $ Js.objectProperty2 k (Js.array [])
_ -> Just $ Js.objectProperty2 k (f x)

listProperty :: forall a. String -> (L.List a -> Js.Tree) -> L.List a -> Maybe Js.ObjectProperty
listProperty k f x =
case x of
Expand Down
5 changes: 3 additions & 2 deletions aeria-compiler/src/Aeria/Semantic.purs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ sSecurity functions = traverse_ go
else throwDiagnostic span $ "Value \""<> strategy <> "\" is not assignable to strategy"
sLogging _ = pure unit

sRequired :: CollectionName -> CollectionRequired -> SemanticM Unit
sRequired collectionName@(CollectionName _ collectionName') = traverse_ go
sRequired :: CollectionName -> Maybe CollectionRequired -> SemanticM Unit
sRequired _ Nothing = pure unit
sRequired collectionName@(CollectionName _ collectionName') (Just required) = traverse_ go required
where
go (Required _ propertyName@(PropertyName span name) cond) = do
context <- ask
Expand Down
4 changes: 2 additions & 2 deletions aeria-compiler/src/Aeria/Syntax/Parser.purs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pPropertyType p = fix \self -> choice
let properties = unsafeCoerce $ getParserValue "properties" results
let additionalProperties = unsafeCoerce $ getParserValue "additionalProperties" results
end <- sourcePos
pure $ PObject (Span begin end) (fromMaybe L.Nil required) (fromMaybe L.Nil properties) additionalProperties
pure $ PObject (Span begin end) required (fromMaybe L.Nil properties) additionalProperties

tStr = pSimpleType PString "str"
tBool = pSimpleType PBoolean "bool"
Expand Down Expand Up @@ -867,6 +867,7 @@ pCollection = go
, search
, immutable
, temporary
, required
, preferred: fromMaybe L.Nil preferred
, actions: fromMaybe L.Nil actions
, formLayout: fromMaybe L.Nil formLayout
Expand All @@ -876,7 +877,6 @@ pCollection = go
, functions: fromMaybe L.Nil functions
, writable: fromMaybe L.Nil writable
, properties: fromMaybe L.Nil properties
, required: fromMaybe L.Nil required
, table: fromMaybe L.Nil table
, getters: fromMaybe L.Nil getters
, tableMeta: fromMaybe L.Nil tableMeta
Expand Down
10 changes: 5 additions & 5 deletions aeria-compiler/src/Aeria/Syntax/Tree.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Data.Either (Either)
import Data.Eq.Generic (genericEq)
import Data.Generic.Rep (class Generic)
import Data.List (List, toUnfoldable)
import Data.Maybe (Maybe)
import Data.Maybe (Maybe(..))
import Data.Ord.Generic (genericCompare)
import Data.Show.Generic (genericShow)
import Data.String.Utils (ucLower)
Expand Down Expand Up @@ -321,7 +321,7 @@ data PropertyType
| PConst Span
| PRef Span CollectionName
| PArray Span PropertyType
| PObject Span CollectionRequired CollectionProperties (Maybe AdditionalProperties)
| PObject Span (Maybe CollectionRequired) CollectionProperties (Maybe AdditionalProperties)

derive instance genericPropertyType :: Generic PropertyType _

Expand Down Expand Up @@ -370,7 +370,7 @@ instance WriteForeign PropertyType
writeImpl (PObject _ required properties _) =
writeImpl
{ kind: "PObject"
, required: writeImpl (toUnfoldable required :: Array Required)
, required: writeImpl (map (toUnfoldable :: _ -> Array Required) required)
, properties: writeImpl (toUnfoldable properties :: Array Property)
}

Expand Down Expand Up @@ -1088,7 +1088,7 @@ data Collection
, individualActions :: CollectionIndividualActions
, security :: CollectionSecurity
, properties :: CollectionProperties
, required :: CollectionRequired
, required :: Maybe CollectionRequired
, getters :: CollectionGetters
, table :: CollectionTable
, tableMeta :: CollectionTableMeta
Expand Down Expand Up @@ -1154,7 +1154,7 @@ instance WriteForeign Collection where
, individualActions: writeImpl (toUnfoldable individualActions :: Array ActionItem)
, security: writeImpl (toUnfoldable security :: Array SecurityItem)
, properties: writeImpl (toUnfoldable properties :: Array Property)
, required: writeImpl (toUnfoldable required :: Array Required)
, required: writeImpl (map (toUnfoldable :: _ -> Array Required) required)
, getters: writeImpl (toUnfoldable getters :: Array Getter)
, table: writeImpl (toUnfoldable table :: Array PropertyName)
, tableMeta: writeImpl (toUnfoldable tableMeta :: Array PropertyName)
Expand Down

0 comments on commit 8f8b8a7

Please sign in to comment.