Skip to content

Commit

Permalink
Extract build_assert_func and call at end to include last error class
Browse files Browse the repository at this point in the history
  • Loading branch information
jackc committed Oct 24, 2020
1 parent 84dd925 commit abadba4
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@
last_cls = ""
last_cls_full = ""

def build_assert_func(last_cls, last_cls_full, cls_errs)
<<~GO
// Is#{last_cls} asserts the error code class is #{last_cls_full}
func Is#{last_cls} (code string) bool {
switch code{
case #{cls_errs.join(", ")}:
return true
}
return false
}
GO
end

puts "// Package pgerrcode contains constants for PostgreSQL error codes."
puts "package pgerrcode"
puts ""
Expand All @@ -47,16 +60,8 @@
case line
when /^Class/
if cls_errs.length > 0 && last_cls != ""
this_cls_errs = cls_errs.join(", ")
assert_func = "// Is#{last_cls} asserts the error code class is #{last_cls_full}\n" \
"func Is#{last_cls} (code string) bool {\n" \
" switch code{\n" \
" case #{this_cls_errs}:\n" \
" return true\n" \
" }\n" \
" return false\n" \
"}\n"
cls_assertions.push(assert_func)
assert_func = build_assert_func(last_cls, last_cls_full, cls_errs)
cls_assertions.push(assert_func)
end
last_cls = line.split("—")[1]
.gsub(" ", "")
Expand Down Expand Up @@ -84,6 +89,12 @@
end
end
puts ")"

if cls_errs.length > 0
assert_func = build_assert_func(last_cls, last_cls_full, cls_errs)
cls_assertions.push(assert_func)
end

cls_assertions.each do |cls_assertion|
puts cls_assertion
end
end

0 comments on commit abadba4

Please sign in to comment.