2
2
3
3
import Dates
4
4
5
+ import Base: @invokelatest
5
6
import .. isvalid_barekey_char
6
7
7
8
function printkey (io:: IO , keys:: Vector{String} )
@@ -20,46 +21,36 @@ function printkey(io::IO, keys::Vector{String})
20
21
end
21
22
22
23
const MbyFunc = Union{Function, Nothing}
23
- const TOMLValue = Union{AbstractVector, AbstractDict, Dates. DateTime, Dates. Time, Dates. Date, Bool, Integer, AbstractFloat, String }
24
- function printvalue (f:: MbyFunc , io:: IO , value:: AbstractVector ; sorted= false )
24
+ const TOMLValue = Union{AbstractVector, AbstractDict, Dates. DateTime, Dates. Time, Dates. Date, Bool, Integer, AbstractFloat, AbstractString }
25
+ function printvalue (f:: MbyFunc , io:: IO , value:: AbstractVector ; sorted= false , by = identity )
25
26
Base. print (io, " [" )
26
27
for (i, x) in enumerate (value)
27
28
i != 1 && Base. print (io, " , " )
28
29
if isa (x, AbstractDict)
29
- _print (f, io, x; sorted)
30
+ _print (f, io, x; sorted, by )
30
31
else
31
- printvalue (f, io, x; sorted)
32
+ printvalue (f, io, x; sorted, by )
32
33
end
33
34
end
34
35
Base. print (io, " ]" )
35
36
end
36
- function printvalue (f:: MbyFunc , io:: IO , value; sorted)
37
- if f === nothing
38
- error (" type `$(typeof (value)) ` is not a valid TOML type, pass a conversion function to `TOML.print`" )
39
- end
40
- toml_value = f (value)
41
- if ! (toml_value isa TOMLValue)
42
- error (" TOML syntax function for type `$(typeof (value)) ` did not return a valid TOML type but a `$(typeof (toml_value)) `" )
43
- end
44
- Base. invokelatest (printvalue, f, io, toml_value; sorted)
45
- end
46
- printvalue (f:: MbyFunc , io:: IO , value:: AbstractDict ; sorted) =
47
- _print (f, io, value; sorted)
48
- printvalue (f:: MbyFunc , io:: IO , value:: Dates.DateTime ; sorted) =
37
+ printvalue (f:: MbyFunc , io:: IO , value:: AbstractDict ; sorted= false , by= identity) =
38
+ _print (f, io, value; sorted, by)
39
+ printvalue (f:: MbyFunc , io:: IO , value:: Dates.DateTime ; _... ) =
49
40
Base. print (io, Dates. format (value, Dates. dateformat " YYYY-mm-dd\T HH:MM:SS.sss\Z " ))
50
- printvalue (f:: MbyFunc , io:: IO , value:: Dates.Time ; sorted ) =
41
+ printvalue (f:: MbyFunc , io:: IO , value:: Dates.Time ; _ ... ) =
51
42
Base. print (io, Dates. format (value, Dates. dateformat " HH:MM:SS.sss" ))
52
- printvalue (f:: MbyFunc , io:: IO , value:: Dates.Date ; sorted ) =
43
+ printvalue (f:: MbyFunc , io:: IO , value:: Dates.Date ; _ ... ) =
53
44
Base. print (io, Dates. format (value, Dates. dateformat " YYYY-mm-dd" ))
54
- printvalue (f:: MbyFunc , io:: IO , value:: Bool ; sorted ) =
45
+ printvalue (f:: MbyFunc , io:: IO , value:: Bool ; _ ... ) =
55
46
Base. print (io, value ? " true" : " false" )
56
- printvalue (f:: MbyFunc , io:: IO , value:: Integer ; sorted ) =
47
+ printvalue (f:: MbyFunc , io:: IO , value:: Integer ; _ ... ) =
57
48
Base. print (io, Int64 (value)) # TOML specifies 64-bit signed long range for integer
58
- printvalue (f:: MbyFunc , io:: IO , value:: AbstractFloat ; sorted ) =
49
+ printvalue (f:: MbyFunc , io:: IO , value:: AbstractFloat ; _ ... ) =
59
50
Base. print (io, isnan (value) ? " nan" :
60
51
isinf (value) ? string (value > 0 ? " +" : " -" , " inf" ) :
61
52
Float64 (value)) # TOML specifies IEEE 754 binary64 for float
62
- printvalue (f:: MbyFunc , io:: IO , value:: AbstractString ; sorted ) = Base. print (io, " \" " , escape_string (value), " \" " )
53
+ printvalue (f:: MbyFunc , io:: IO , value:: AbstractString ; _ ... ) = Base. print (io, " \" " , escape_string (value), " \" " )
63
54
64
55
is_table (value) = isa (value, AbstractDict)
65
56
is_array_of_tables (value) = isa (value, AbstractArray) &&
@@ -70,8 +61,8 @@ function _print(f::MbyFunc, io::IO, a::AbstractDict,
70
61
ks:: Vector{String} = String[];
71
62
indent:: Int = 0 ,
72
63
first_block:: Bool = true ,
73
- sorted:: Bool ,
74
- by:: Function ,
64
+ sorted:: Bool = false ,
65
+ by:: Function = identity ,
75
66
)
76
67
akeys = keys (a)
77
68
if sorted
@@ -82,11 +73,25 @@ function _print(f::MbyFunc, io::IO, a::AbstractDict,
82
73
for key in akeys
83
74
value = a[key]
84
75
is_tabular (value) && continue
85
- Base. print (io, ' ' ^ 4 max (0 ,indent- 1 ))
86
- printkey (io, [String (key)])
87
- Base. print (io, " = " ) # print separator
88
- printvalue (f, io, value; sorted)
89
- Base. print (io, " \n " ) # new line?
76
+ if ! isa (value, TOMLValue)
77
+ if f === nothing
78
+ error (" type `$(typeof (value)) ` is not a valid TOML type, pass a conversion function to `TOML.print`" )
79
+ end
80
+ toml_value = f (value)
81
+ if ! (toml_value isa TOMLValue)
82
+ error (" TOML syntax function for type `$(typeof (value)) ` did not return a valid TOML type but a `$(typeof (toml_value)) `" )
83
+ end
84
+ value = toml_value
85
+ end
86
+ if is_tabular (value)
87
+ _print (f, io, Dict (key => value); indent, first_block, sorted, by)
88
+ else
89
+ Base. print (io, ' ' ^ 4 max (0 ,indent- 1 ))
90
+ printkey (io, [String (key)])
91
+ Base. print (io, " = " ) # print separator
92
+ printvalue (f, io, value; sorted, by)
93
+ Base. print (io, " \n " ) # new line?
94
+ end
90
95
first_block = false
91
96
end
92
97
@@ -105,7 +110,7 @@ function _print(f::MbyFunc, io::IO, a::AbstractDict,
105
110
Base. print (io," ]\n " )
106
111
end
107
112
# Use runtime dispatch here since the type of value seems not to be enforced other than as AbstractDict
108
- Base . invokelatest (_print, f, io, value, ks; indent = indent + header, first_block = header, sorted, by)
113
+ @ invokelatest _print ( f, io, value, ks; indent = indent + header, first_block = header, sorted, by)
109
114
pop! (ks)
110
115
elseif is_array_of_tables (value)
111
116
# print array of tables
@@ -119,7 +124,7 @@ function _print(f::MbyFunc, io::IO, a::AbstractDict,
119
124
Base. print (io," ]]\n " )
120
125
# TODO , nicer error here
121
126
! isa (v, AbstractDict) && error (" array should contain only tables" )
122
- Base . invokelatest (_print, f, io, v, ks; indent = indent + 1 , sorted, by)
127
+ @ invokelatest _print ( f, io, v, ks; indent = indent + 1 , sorted, by)
123
128
end
124
129
pop! (ks)
125
130
end
0 commit comments