@@ -2,21 +2,20 @@ import backend/index/error
2
2
import gleam/bool
3
3
import gleam/dict . { type Dict }
4
4
import gleam/dynamic
5
+ import gleam/generate/sources . { type_definition_to_string }
6
+ import gleam/generate/types . { type_definition_to_json }
5
7
import gleam/int
6
- import gleam/iterator
7
- import gleam/json . { type Json }
8
+ import gleam/io
9
+ import gleam/json
8
10
import gleam/list
9
11
import gleam/option . { None , Some }
10
- import gleam/package_interface . {
11
- type Module , type Package , type Parameter , type Type , type TypeConstructor ,
12
- type TypeDefinition ,
13
- }
12
+ import gleam/package_interface . { type Module , type Package }
14
13
import gleam/pgo
15
14
import gleam/result
16
15
import gleam/string
17
16
import pprint
18
- import ranger
19
17
import tom . { type Toml }
18
+ import wisp
20
19
21
20
fn add_gleam_constraint ( db : pgo . Connection , package : Package , release_id : Int ) {
22
21
case package . gleam_version_constraint {
@@ -74,175 +73,92 @@ fn upsert_package_module(
74
73
|> result . replace_error ( error . UnknownError ( "No module" ) )
75
74
}
76
75
77
- fn generate_type_definition_signature (
78
- type_name : String ,
79
- type_def : TypeDefinition ,
76
+ fn upsert_type_definitions (
77
+ db : pgo . Connection ,
78
+ module_id : Int ,
79
+ module : Module ,
80
+ gleam_toml : Dict ( String , Toml ) ,
80
81
) {
81
- let params = case type_def . parameters {
82
- 0 -> ""
83
- _ -> {
84
- let range =
85
- ranger . create_infinite (
86
- validate : fn ( a ) { string . length ( a ) == 1 } ,
87
- add : fn ( a : String , b : Int ) {
88
- let assert [ code ] = string . to_utf_codepoints ( a )
89
- let int_code = string . utf_codepoint_to_int ( code )
90
- let new_int_code = int_code + b
91
- let assert Ok ( new_code ) = string . utf_codepoint ( new_int_code )
92
- string . from_utf_codepoints ( [ new_code ] )
93
- } ,
94
- compare : string . compare ,
95
- )
96
- let assert Ok ( from_a ) = range ( "a" , 1 )
97
- from_a
98
- |> iterator . take ( type_def . parameters )
99
- |> iterator . to_list ( )
100
- |> string . join ( ", " )
101
- |> fn ( s ) { "(" <> s <> ")" }
102
- }
103
- }
104
- let base = type_name <> params
105
- case type_def . constructors {
106
- [ ] -> base
107
- items -> base <> " {\n " <> generate_type_constructors ( items ) <> "\n }"
108
- }
109
- }
110
-
111
- fn generate_type_constructors ( constructors : List ( TypeConstructor ) ) {
112
- constructors
113
- |> list . map ( fn ( c ) {
114
- let parameters = generate_parameters ( c . parameters )
115
- let const_ = " " <> c . name <> parameters
116
- case c . documentation {
117
- None -> const_
118
- Some ( d ) -> string . join ( [ " -- " <> d , const_ ] , "\n " )
119
- }
120
- } )
121
- |> string . join ( "\n " )
122
- }
82
+ let all_types = dict . to_list ( module . types )
83
+ result . all ( {
84
+ use # ( type_name , type_def ) <- list . map ( all_types )
85
+ let documentation = string . trim ( option . unwrap ( type_def . documentation , "" ) )
86
+ let metadata =
87
+ type_def . deprecation
88
+ |> option . map ( fn ( d ) { json . string ( d . message ) } )
89
+ |> option . map ( fn ( d ) { json . object ( [ # ( "deprecation" , d ) ] ) } )
90
+ |> option . map ( json . to_string )
91
+ |> option . unwrap ( "{}" )
92
+ let signature = type_definition_to_string ( type_name , type_def )
93
+ let type_def_json =
94
+ type_definition_to_json ( db , type_name , type_def , gleam_toml )
95
+ use # ( json_signature , parameters ) <- result . try ( type_def_json )
96
+ pprint . debug (
97
+ json_signature
98
+ |> json . to_string ( ) ,
99
+ )
100
+ let params = parameters
123
101
124
- fn generate_parameters ( parameters : List ( Parameter ) ) {
125
- use <- bool . guard ( when : list . is_empty ( parameters ) , return : "" )
126
- parameters
127
- |> list . map ( fn ( s ) {
128
- let label =
129
- s . label
130
- |> option . map ( string . append ( _, ": " ) )
131
- |> option . unwrap ( "" )
132
- label <> generate_type ( s . type_ )
102
+ "INSERT INTO package_type_fun_signature (
103
+ name,
104
+ documentation,
105
+ signature_,
106
+ json_signature,
107
+ nature,
108
+ parameters,
109
+ metadata,
110
+ package_module_id,
111
+ deprecation
112
+ ) VALUES ($1, $2, $3, $4, 'type_definition', $5, $6, $7, $8)
113
+ ON CONFLICT (package_module_id, name) DO UPDATE
114
+ SET
115
+ documentation = $2,
116
+ signature_ = $3,
117
+ json_signature = $4,
118
+ nature = 'type_definition',
119
+ parameters = $5,
120
+ metadata = $6,
121
+ deprecation = $8"
122
+ |> pgo . execute (
123
+ db ,
124
+ [
125
+ pgo . text ( type_name ) ,
126
+ pgo . text ( documentation ) ,
127
+ pgo . text ( signature ) ,
128
+ json_signature
129
+ |> json . to_string ( )
130
+ |> pgo . text ( ) ,
131
+ dynamic . unsafe_coerce ( dynamic . from ( params ) ) ,
132
+ pgo . text ( metadata ) ,
133
+ pgo . int ( module_id ) ,
134
+ type_def . deprecation
135
+ |> option . map ( fn ( d ) { d . message } )
136
+ |> pgo . nullable ( pgo . text , _) ,
137
+ ] ,
138
+ dynamic . dynamic ,
139
+ )
140
+ |> result . map_error ( error . DatabaseError )
141
+ |> result . replace ( Nil )
133
142
} )
134
- |> string . join ( ", " )
135
- |> fn ( s ) { "(" <> s <> ")" }
136
- }
137
-
138
- fn generate_type ( type_ : Type ) {
139
- case type_ {
140
- package_interface . Tuple ( elements ) -> {
141
- let els =
142
- elements
143
- |> list . map ( generate_type )
144
- |> string . join ( ", " )
145
- "#(" <> els <> ")"
146
- }
147
- package_interface . Fn ( parameters , return ) -> {
148
- let ret = generate_type ( return )
149
- let params =
150
- parameters
151
- |> list . map ( generate_type )
152
- |> string . join ( ", " )
153
- "fn(" <> params <> ") -> " <> ret
154
- }
155
- package_interface . Variable ( id ) -> {
156
- let assert Ok ( utf_a ) =
157
- "a"
158
- |> string . to_utf_codepoints ( )
159
- |> list . first ( )
160
- { string . utf_codepoint_to_int ( utf_a ) + id }
161
- |> string . utf_codepoint ( )
162
- |> result . map ( list . prepend ( [ ] , _) )
163
- |> result . map ( string . from_utf_codepoints )
164
- |> result . unwrap ( "a" )
165
- }
166
- package_interface . Named ( name , package , module , parameters ) -> {
167
- let params =
168
- parameters
169
- |> list . map ( generate_type )
170
- |> string . join ( ", " )
171
- |> fn ( s ) {
172
- use <- bool . guard ( when : string . is_empty ( s ) , return : s )
173
- "(" <> s <> ")"
174
- }
175
- case package {
176
- "" -> name <> params
177
- _ -> module <> "." <> name <> params
178
- }
179
- }
180
- }
181
- }
182
-
183
- fn generate_type_definition_json_signature (
184
- type_name : String ,
185
- type_def : TypeDefinition ,
186
- ) -> Result ( # ( Json , List ( Int ) ) , error . Error ) {
187
- Ok ( # ( json . object ( [ ] ) , [ ] ) )
188
143
}
189
144
190
145
pub fn extract_signatures (
191
146
db : pgo . Connection ,
192
147
package : Package ,
193
- toml : Dict ( String , Toml ) ,
148
+ gleam_toml : Dict ( String , Toml ) ,
194
149
) {
195
150
use # ( _pid , rid ) <- result . try ( get_package_release_ids ( db , package ) )
196
151
use _ <- result . try ( add_gleam_constraint ( db , package , rid ) )
197
152
package . modules
198
- |> dict . map_values ( fn ( mod_name , module ) {
199
- pprint . debug ( "Inserting " <> mod_name )
153
+ |> dict . to_list ( )
154
+ |> list . map ( fn ( mod ) {
155
+ let # ( mod_name , module ) = mod
156
+ wisp . log_info ( "Inserting " <> mod_name )
200
157
use module_id <- result . try ( upsert_package_module ( db , mod_name , module , rid ) )
201
- module . types
202
- |> dict . map_values ( fn ( type_name , type_def ) {
203
- let documentation =
204
- option . unwrap ( type_def . documentation , "" )
205
- |> string . trim ( )
206
- let metadata =
207
- type_def . deprecation
208
- |> option . map ( fn ( d ) {
209
- json . object ( [ # ( "deprecation" , json . string ( d . message ) ) ] )
210
- } )
211
- |> option . map ( json . to_string )
212
- |> option . unwrap ( "{}" )
213
- let signature = generate_type_definition_signature ( type_name , type_def )
214
- use # ( json_signature , parameters ) <- result . try (
215
- generate_type_definition_json_signature ( type_name , type_def ) ,
216
- )
217
- let params =
218
- parameters
219
- |> list . map ( int . to_string )
220
- |> string . join ( ", " )
221
- |> fn ( v ) { "[" <> v <> "]" }
222
- pprint . debug ( signature )
223
- Ok ( Nil )
224
- } // "INSERT INTO package_type_fun_signature (name, documentation, signature_, json_signature, nature, parameters, metadata, package_module_id)
225
- // VALUES ($1, $2, $3, $4, 'type_definition', $5, $6, $7)
226
- // ON CONFLICT (package_module_id, name) DO UPDATE
227
- // SET documentation = $2, signature_ = $3, json_signature = $4, nature = 'type_definition', parameters = $5, metadata = $6"
228
- // |> pgo.execute(
229
- // db,
230
- // [
231
- // pgo.text(type_name),
232
- // pgo.text(documentation),
233
- // pgo.text(signature),
234
- // json_signature
235
- // |> json.to_string()
236
- // |> pgo.text(),
237
- // pgo.text(params),
238
- // pgo.text(metadata),
239
- // pgo.int(module_id),
240
- // ],
241
- // dynamic.dynamic,
242
- // )
243
- // |> result.map_error(error.DatabaseError)
244
- )
245
- Ok ( Nil )
158
+ module
159
+ |> upsert_type_definitions ( db , module_id , _, gleam_toml )
160
+ |> result . replace ( Nil )
246
161
} )
247
- Ok ( Nil )
162
+ |> result . all ( )
163
+ |> io . debug ( )
248
164
}
0 commit comments