Skip to content

Commit d67ba7a

Browse files
authored
move pgzx.fmgr.conv to pgzx.datum (#27)
I think the 'datum' converters should become reusable in general. Let's move them into its own top level module and cleanup some module re-exports in fmgr.
1 parent 6329967 commit d67ba7a

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

src/pgzx.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub const HTabIter = collections.htab.HTabIter;
2020
pub const StringHashTable = collections.htab.StringHashTable;
2121
pub const KVHashTable = collections.htab.KVHashTable;
2222

23+
pub const datum = @import("pgzx/datum.zig");
24+
2325
pub const elog = @import("pgzx/elog.zig");
2426

2527
pub const err = @import("pgzx/err.zig");

src/pgzx/fmgr/conv.zig renamed to src/pgzx/datum.zig

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const std = @import("std");
22

3-
const c = @import("../c.zig");
4-
const err = @import("../err.zig");
5-
const mem = @import("../mem.zig");
6-
const meta = @import("../meta.zig");
7-
const varatt = @import("../varatt.zig");
3+
const c = @import("c.zig");
4+
const err = @import("err.zig");
5+
const mem = @import("mem.zig");
6+
const meta = @import("meta.zig");
7+
const varatt = @import("varatt.zig");
88

99
pub fn Conv(comptime T: type, comptime from: anytype, comptime to: anytype) type {
1010
return struct {
@@ -74,18 +74,18 @@ var directMappings = .{
7474
};
7575

7676
pub fn fromNullableDatum(comptime T: type, d: c.NullableDatum) !T {
77-
return find(T).fromNullableDatum(d);
77+
return findConv(T).fromNullableDatum(d);
7878
}
7979

8080
pub fn fromDatum(comptime T: type, d: c.Datum, is_null: bool) !T {
81-
return find(T).fromNullableDatum(.{ .value = d, .isnull = is_null });
81+
return findConv(T).fromNullableDatum(.{ .value = d, .isnull = is_null });
8282
}
8383

8484
pub fn toNullableDatum(v: anytype) !c.NullableDatum {
85-
return find(@TypeOf(v)).toNullableDatum(v);
85+
return findConv(@TypeOf(v)).toNullableDatum(v);
8686
}
8787

88-
pub fn find(comptime T: type) type {
88+
pub fn findConv(comptime T: type) type {
8989
if (isConv(T)) { // is T already a converter?
9090
return T;
9191
}
@@ -124,7 +124,7 @@ pub fn find(comptime T: type) type {
124124
64 => Float64,
125125
else => @compileError("unsupported float type"),
126126
},
127-
.Optional => |opt| OptConv(find(opt.child)),
127+
.Optional => |opt| OptConv(findConv(opt.child)),
128128
.Array => @compileLog("fixed size arrays not supported"),
129129
.Pointer => blk: {
130130
if (!meta.isStringLike(T)) {

src/pgzx/fmgr.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const std = @import("std");
22

33
const c = @import("c.zig");
4-
pub const elog = @import("elog.zig");
4+
const elog = @import("elog.zig");
5+
const datum = @import("datum.zig");
6+
57
pub const args = @import("fmgr/args.zig");
6-
pub const conv = @import("fmgr/conv.zig");
78
pub const varatt = c.varatt;
89

910
pub const Pg_magic_struct = c.Pg_magic_struct;
@@ -93,7 +94,7 @@ pub inline fn pgCall(
9394
}
9495

9596
const value = @call(.no_async, impl, callArgs) catch |e| elog.throwAsPostgresError(src, e);
96-
const resultConv = conv.find(@TypeOf(value));
97+
const resultConv = datum.findConv(@TypeOf(value));
9798
const nullableDatum = resultConv.toNullableDatum(value) catch |e| elog.throwAsPostgresError(src, e);
9899
if (nullableDatum.isnull) {
99100
fcinfo.*.isnull = true;

src/pgzx/fmgr/args.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const std = @import("std");
22
const c = @import("../c.zig");
3-
const conv = @import("conv.zig");
3+
const datum = @import("../datum.zig");
44

55
/// Index function argument type.
66
pub fn Arg(comptime T: type, comptime argNum: u32) type {
@@ -49,15 +49,15 @@ inline fn readArgType(comptime T: type) type {
4949
if (T == c.FunctionCallInfo) {
5050
return T;
5151
}
52-
return conv.find(T).Type;
52+
return datum.findConv(T).Type;
5353
}
5454

5555
/// Reads a postgres function call argument as a given type.
5656
fn readArg(comptime T: type, fcinfo: c.FunctionCallInfo, argNum: u32) !readArgType(T) {
5757
if (T == c.FunctionCallInfo) {
5858
return fcinfo;
5959
}
60-
const converter = comptime conv.find(T);
60+
const converter = comptime datum.findConv(T);
6161
return converter.fromNullableDatum(try mustGetArgNullable(fcinfo, argNum));
6262
}
6363

0 commit comments

Comments
 (0)