@@ -28,6 +28,33 @@ defmodule Sheetshow.Records do
2828 @ doc "A tab's columns, left to right: the two reserved ones, then the schema's."
2929 def columns ( schema ) , do: [ @ id , @ deleted | Schema . columns ( schema ) ]
3030
31+ @ doc """
32+ Validates a schema for a database model: one `Sheetshow.Schema.validate/1`
33+ accepts, and none of whose columns is named `id` or `deleted`. Those two are
34+ the model's own, kept for a row's identity and its tombstone flag, so a schema
35+ column by either name would sit under the reserved one and be read as it, and
36+ writing the record would overwrite the identity. `Log` and `Table` validate
37+ through here rather than through `Schema` directly.
38+ """
39+ def validate_schema ( schema ) do
40+ with :ok <- Schema . validate ( schema ) do
41+ case Enum . find ( Schema . columns ( schema ) , & ( String . downcase ( & 1 ) in [ @ id , @ deleted ] ) ) do
42+ nil -> :ok
43+ name -> { :error , reserved ( name ) }
44+ end
45+ end
46+ end
47+
48+ defp reserved ( name ) do
49+ Error . new (
50+ :invalid_schema ,
51+ "#{ inspect ( name ) } is a reserved column: #{ inspect ( @ id ) } and #{ inspect ( @ deleted ) } are the " <>
52+ "model's own, kept for every row's identity and its tombstone flag, so name the column " <>
53+ "something else" ,
54+ column: name
55+ )
56+ end
57+
3158 @ doc "The header row as cells, for the tab a `%Log{}` or `%Table{}` names."
3259 def header ( % { sheet: sheet , schema: schema } , style ) do
3360 schema |> columns ( ) |> Sheetshow . row ( sheet: sheet , style: style )
@@ -69,13 +96,14 @@ defmodule Sheetshow.Records do
6996 in the same places.
7097 """
7198 def index ( header , sheet , schema ) do
72- positions =
99+ indexed =
73100 for { name , position } <- Enum . with_index ( header ) ,
74101 key = normalise ( name ) ,
75102 key != nil ,
76- into: % { } do
77- { key , position }
78- end
103+ do: { key , position }
104+
105+ counts = indexed |> Enum . map ( & elem ( & 1 , 0 ) ) |> Enum . frequencies ( )
106+ positions = Map . new ( indexed )
79107
80108 if positions == % { } do
81109 { :error ,
@@ -87,9 +115,21 @@ defmodule Sheetshow.Records do
87115 ) }
88116 else
89117 Result . reduce ( columns ( schema ) , % { } , fn name , acc ->
90- case Map . fetch ( positions , String . downcase ( name ) ) do
91- { :ok , position } -> { :ok , Map . put ( acc , name , position ) }
92- :error -> { :error , missing_column ( name , sheet , header ) }
118+ key = String . downcase ( name )
119+
120+ cond do
121+ # A tab a person edits can end up with a column named twice. Which one
122+ # holds the truth is a question only they can answer, so it is refused
123+ # rather than answered by taking the last, which is what building the
124+ # map used to do silently.
125+ Map . get ( counts , key , 0 ) > 1 ->
126+ { :error , duplicate_column ( name , sheet , header ) }
127+
128+ ( position = Map . get ( positions , key ) ) != nil ->
129+ { :ok , Map . put ( acc , name , position ) }
130+
131+ true ->
132+ { :error , missing_column ( name , sheet , header ) }
93133 end
94134 end )
95135 end
@@ -114,6 +154,17 @@ defmodule Sheetshow.Records do
114154 )
115155 end
116156
157+ defp duplicate_column ( name , sheet , header ) do
158+ Error . new (
159+ :duplicate_column ,
160+ "#{ inspect ( sheet ) } names the #{ inspect ( name ) } column more than once, so there is no " <>
161+ "telling which to read or write: its header reads #{ inspect ( header ) } " ,
162+ column: name ,
163+ sheet: sheet ,
164+ header: header
165+ )
166+ end
167+
117168 @ doc """
118169 A cell for a row written outside the planner (an append, or a write to a
119170 known row), with the number format a date needs, which those paths would
0 commit comments