Skip to content

Commit

Permalink
Fix nodetype for Node and Edge in load_table.
Browse files Browse the repository at this point in the history
  • Loading branch information
evetion committed Oct 24, 2023
1 parent e105832 commit 7dc3b5f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
6 changes: 2 additions & 4 deletions core/src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function load_data(
schema = Legolas._schema_version_from_record_type(record)

node, kind = nodetype(schema)
path = getfield(getfield(config, snake_case(node)), kind)
path = isnothing(kind) ? nothing : getfield(getfield(config, snake_case(node)), kind)
sqltable = tablename(schema)

table = if path !== nothing
Expand Down Expand Up @@ -113,10 +113,8 @@ function load_structvector(
tableschema = Tables.schema(table)
if declared(sv) && tableschema !== nothing
validate(tableschema, sv)
# R = Legolas.record_type(sv)
# foreach(R, Tables.rows(table)) # construct each row
else
@warn "No (validation) schema declared for $nodetype $kind"
@warn "No (validation) schema declared for $T"
end

return sorted_table!(table)
Expand Down
18 changes: 15 additions & 3 deletions core/src/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,26 @@ nodetype(sv::Type{SchemaVersion{T, N}}) where {T, N} = nodetype(sv())
"""
From a SchemaVersion("ribasim.flowboundary.static", 1) return (:FlowBoundary, :static)
"""
function nodetype(sv::SchemaVersion{T, N})::Tuple{Symbol, Symbol} where {T, N}
n, k = split(string(T), ".")[2:3]
function nodetype(
sv::SchemaVersion{T, N},
)::Tuple{Symbol, Union{Nothing, Symbol}} where {T, N}
# Names derived from a schema are in underscores (basintime),
# so we parse the related record Ribasim.BasinTimeV1
# to derive BasinTime from it.
record = Legolas.record_type(sv)
node = last(split(string(Symbol(record)), "."))
return Symbol(node[begin:length(n)]), Symbol(k)
elements = split(string(T), ".")

if length(elements) == 3
# Ribasim.Node.kind schema
kind = Symbol(elements[3])
else
# Or Just a Ribasim.Node/Edge schema
kind = nothing
end

# Strip V1 from the end
return Symbol(node[begin:(end - 2)]), kind
end

# Allowed types for downstream (to_node_id) nodes given the type of the upstream (from_node_id) node
Expand Down

0 comments on commit 7dc3b5f

Please sign in to comment.