on dev help sign in
generic constructor
commite538877eab53baec31258fa2bf9ffd7182974579 authorEitan Chatav <eitan.chatav@gmail.com> dateFri 18 May 2018, 19:56 (8 years ago) parent5b9374f
1 file changed+15−2
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+15−2
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+15−2
@@ -121,10 +121,23 @@ instance (HasOid pg, ToParam x pg)
121121 (oid @pg) (unK . toParam @x @pg)
122122instance
123123 ( IsEnumType x
124− , Show x
124+ , HasDatatypeInfo x
125125 , SameLabels (DatatypeInfoOf x) labels
126126 ) => ToParam x ('PGenum labels) where
127− toParam = K . Encoding.text_strict . Strict.pack . show
127+ toParam =
128+ let
129+ gcons :: forall y. (HasDatatypeInfo y, Generic y) => y -> String
130+ gcons = gconstructor (constructorInfo (datatypeInfo (Proxy @y))) . from
131+
132+ gconstructor :: NP ConstructorInfo xs -> SOP I xs -> String
133+ gconstructor Nil _ = ""
134+ gconstructor (Constructor name :* _) (SOP (Z _)) = name
135+ gconstructor (Infix name _ _ :* _) (SOP (Z _)) = name
136+ gconstructor (Record name _ :* _) (SOP (Z _)) = name
137+ gconstructor (_ :* constructors) (SOP (S xs)) =
138+ gconstructor constructors (SOP xs)
139+ in
140+ K . Encoding.text_strict . Strict.pack . gcons
128141instance
129142 ( SListI fields
130143 , MapMaybes xs