Skip to content

Commit

Permalink
move pgzx.fmgr.conv to pgzx.datum
Browse files Browse the repository at this point in the history
  • Loading branch information
urso authored and urso committed Mar 20, 2024
1 parent 9825dde commit cab6afc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/pgzx.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub const HTabIter = collections.htab.HTabIter;
pub const StringHashTable = collections.htab.StringHashTable;
pub const KVHashTable = collections.htab.KVHashTable;

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

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

pub const err = @import("pgzx/err.zig");
Expand Down
20 changes: 10 additions & 10 deletions src/pgzx/fmgr/conv.zig → src/pgzx/datum.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const std = @import("std");

const c = @import("../c.zig");
const err = @import("../err.zig");
const mem = @import("../mem.zig");
const meta = @import("../meta.zig");
const varatt = @import("../varatt.zig");
const c = @import("c.zig");
const err = @import("err.zig");
const mem = @import("mem.zig");
const meta = @import("meta.zig");
const varatt = @import("varatt.zig");

pub fn Conv(comptime T: type, comptime from: anytype, comptime to: anytype) type {
return struct {
Expand Down Expand Up @@ -74,18 +74,18 @@ var directMappings = .{
};

pub fn fromNullableDatum(comptime T: type, d: c.NullableDatum) !T {
return find(T).fromNullableDatum(d);
return findConv(T).fromNullableDatum(d);
}

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

pub fn toNullableDatum(v: anytype) !c.NullableDatum {
return find(@TypeOf(v)).toNullableDatum(v);
return findConv(@TypeOf(v)).toNullableDatum(v);
}

pub fn find(comptime T: type) type {
pub fn findConv(comptime T: type) type {
if (isConv(T)) { // is T already a converter?
return T;
}
Expand Down Expand Up @@ -124,7 +124,7 @@ pub fn find(comptime T: type) type {
64 => Float64,
else => @compileError("unsupported float type"),
},
.Optional => |opt| OptConv(find(opt.child)),
.Optional => |opt| OptConv(findConv(opt.child)),
.Array => @compileLog("fixed size arrays not supported"),
.Pointer => blk: {
if (!meta.isStringLike(T)) {
Expand Down
7 changes: 4 additions & 3 deletions src/pgzx/fmgr.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const std = @import("std");

const c = @import("c.zig");
pub const elog = @import("elog.zig");
const elog = @import("elog.zig");
const datum = @import("datum.zig");

pub const args = @import("fmgr/args.zig");
pub const conv = @import("fmgr/conv.zig");
pub const varatt = c.varatt;

pub const Pg_magic_struct = c.Pg_magic_struct;
Expand Down Expand Up @@ -93,7 +94,7 @@ pub inline fn pgCall(
}

const value = @call(.no_async, impl, callArgs) catch |e| elog.throwAsPostgresError(src, e);
const resultConv = conv.find(@TypeOf(value));
const resultConv = datum.findConv(@TypeOf(value));
const nullableDatum = resultConv.toNullableDatum(value) catch |e| elog.throwAsPostgresError(src, e);
if (nullableDatum.isnull) {
fcinfo.*.isnull = true;
Expand Down
6 changes: 3 additions & 3 deletions src/pgzx/fmgr/args.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");
const c = @import("../c.zig");
const conv = @import("conv.zig");
const datum = @import("../datum.zig");

/// Index function argument type.
pub fn Arg(comptime T: type, comptime argNum: u32) type {
Expand Down Expand Up @@ -49,15 +49,15 @@ inline fn readArgType(comptime T: type) type {
if (T == c.FunctionCallInfo) {
return T;
}
return conv.find(T).Type;
return datum.findConv(T).Type;
}

/// Reads a postgres function call argument as a given type.
fn readArg(comptime T: type, fcinfo: c.FunctionCallInfo, argNum: u32) !readArgType(T) {
if (T == c.FunctionCallInfo) {
return fcinfo;
}
const converter = comptime conv.find(T);
const converter = comptime datum.findConv(T);
return converter.fromNullableDatum(try mustGetArgNullable(fcinfo, argNum));
}

Expand Down

0 comments on commit cab6afc

Please sign in to comment.