Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message for invalid default value of a union field #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions haskell/compiler/adlc-lib0/ADL/Compiler/Processing.hs
Original file line number Diff line number Diff line change
@@ -28,10 +28,15 @@ import qualified Data.Text.IO as T
import qualified Text.Parsec as P
import qualified Data.Vector as V
import qualified Data.Aeson as JSON
import qualified Data.Aeson.Text as JSON
import qualified Data.Aeson.KeyMap as KM
import qualified Data.Aeson.Key as AKey
import qualified Data.Scientific as S

import qualified Data.Text.Lazy as LT
import qualified Data.Text.Lazy.Builder as LT
import qualified Data.Text.Encoding as T

import qualified ADL.Compiler.ParserP as P

import ADL.Utils.Format
@@ -638,16 +643,16 @@ literalForTypeExpr te v = litForTE Map.empty te v
lit <- litForTE pm ftype v
Right (f_name f,lit)
Nothing ->
Left (T.concat ["Field ",AKey.toText k, " in literal doesn't match any in union definition for", d_name decl])
Left (T.concat ["Object ",AKey.toText k, " in literal doesn't match any in union definition for ", d_name decl])
_ -> Left "literal union must have a single key/value pair"
unionField m decl u tes (JSON.String k) = do
pm <- createParamMap (u_typeParams u) tes m
case find ((k==).f_serializedName) (u_fields u) of
(Just f) | isVoidType (f_type f) -> Right (f_name f,Literal (f_type f) (LPrimitive JSON.Null))
| otherwise -> Left (T.concat ["Field ",k, " in literal for ", d_name decl, " must be an object"])
Nothing ->
Left (T.concat ["Field ",k, " in literal doesn't match any in union definition for", d_name decl])
unionField _ _ _ _ _ = Left "expected an object"
Left (T.concat ["String ",k, " in literal doesn't match any in union definition for ", d_name decl])
unionField _ decl _ _ k = Left (T.concat ["expected an object for ", d_name decl, " received value ", jsonToStr k ])

typedefLiteral m t tes v = do
pm <- createParamMap (t_typeParams t) tes m
@@ -953,3 +958,5 @@ getSerializedWithInternalTag annotations = case Map.lookup serializedWithInterna
customSerialization = ScopedName (ModuleName ["sys","annotations"]) "CustomSerialization"
serializedWithInternalTag = ScopedName (ModuleName ["sys", "annotations"]) "SerializedWithInternalTag"

jsonToStr :: JSON.Value -> T.Text
jsonToStr = LT.toStrict . LT.toLazyText . JSON.encodeToTextBuilder
Loading