Skip to content

Commit d77e89b

Browse files
authored
Merge pull request #16 from jw3126/fastconvert
Fastconvert
2 parents ec2fd64 + 56350d2 commit d77e89b

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

.github/workflows/CI.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@ jobs:
2323
with:
2424
version: ${{ matrix.version }}
2525
arch: ${{ matrix.arch }}
26-
- uses: actions/cache@v1
27-
env:
28-
cache-name: cache-artifacts
29-
with:
30-
path: ~/.julia/artifacts
31-
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
32-
restore-keys: |
33-
${{ runner.os }}-test-${{ env.cache-name }}-
34-
${{ runner.os }}-test-
35-
${{ runner.os }}-
3626
- uses: julia-actions/julia-buildpkg@v1
3727
- uses: julia-actions/julia-runtest@v1
3828
- uses: julia-actions/julia-processcoverage@v1

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "StructHelpers"
22
uuid = "4093c41a-2008-41fd-82b8-e3f9d02b504f"
33
authors = ["Jan Weidner <jw3126@gmail.com> and contributors"]
4-
version = "1.3.1"
4+
version = "1.4.0"
55

66
[deps]
77
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"

src/StructHelpers.jl

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,22 +315,29 @@ end
315315
function enum_from_string end
316316
function enum_from_symbol end
317317
function string_from_enum(x)::String
318+
string_from_enum_generic(x)
319+
end
320+
function string_from_enum_generic(x)::String
318321
string(x)
319322
end
323+
320324
function symbol_from_enum(x)::Symbol
321-
Symbol(string_from_enum(x))
325+
symbol_from_enum_generic(x)
326+
end
327+
function symbol_from_enum_generic(x)::Symbol
328+
Symbol(string_from_enum_generic(x))
322329
end
323330

324331
function def_enum_from_string(T)::Expr
325-
body = def_symbol_or_enum_from_string_body(string_from_enum, T)
332+
body = def_enum_from_string_or_symbol_body(string_from_enum, T)
326333
:(
327334
function $SH.enum_from_string(::Type{$T}, s::String)::$T
328335
$body
329336
end
330337
)
331338
end
332339
function def_enum_from_symbol(T)::Expr
333-
body = def_symbol_or_enum_from_string_body(QuoteNodesymbol_from_enum, T)
340+
body = def_enum_from_string_or_symbol_body(QuoteNodesymbol_from_enum, T)
334341
:(
335342
function $SH.enum_from_symbol(::Type{$T}, s::Symbol)::$T
336343
$body
@@ -348,7 +355,33 @@ end
348355
throw(ArgumentError(msg))
349356
end
350357

351-
function def_symbol_or_enum_from_string_body(f,T)
358+
function def_symbol_or_string_from_enum_body(s_from_enum_generic, T)::Expr
359+
default = :($s_from_enum_generic(obj))
360+
matcharms = [
361+
:(obj === $inst) => QuoteNode(s_from_enum_generic(inst)) for inst in instances(T)
362+
]
363+
ifelsechain(matcharms, default)
364+
end
365+
366+
function def_symbol_from_enum(T)::Expr
367+
body = def_symbol_or_string_from_enum_body(symbol_from_enum_generic, T)
368+
:(
369+
function $SH.symbol_from_enum(obj::$T)::Symbol
370+
$body
371+
end
372+
)
373+
end
374+
375+
function def_string_from_enum(T)::Expr
376+
body = def_symbol_or_string_from_enum_body(string_from_enum_generic, T)
377+
:(
378+
function $SH.string_from_enum(obj::$T)::String
379+
$body
380+
end
381+
)
382+
end
383+
384+
function def_enum_from_string_or_symbol_body(f,T)
352385
err = :($throw_no_matching_instance($f,$T,s))
353386
matcharms = [
354387
:(s === $(f(inst))) => inst for inst in instances(T)
@@ -440,6 +473,8 @@ macro enumbatteries(T, kw...)
440473
push!(ret.args, :(import StructHelpers as $SH))
441474
push!(ret.args, def_enum_from_symbol(TT))
442475
push!(ret.args, def_enum_from_string(TT))
476+
push!(ret.args, def_symbol_from_enum(TT))
477+
push!(ret.args, def_string_from_enum(TT))
443478
if nt.string_conversion
444479
ex1 = :(Base.convert(::Type{$TT}, s::AbstractString) = $SH.enum_from_string($TT, String(s)))
445480
ex2 = :($T(s::AbstractString) = $SH.enum_from_string($TT, String(s)))

0 commit comments

Comments
 (0)