Skip to content

Commit

Permalink
Go in the other direction to jsonb
Browse files Browse the repository at this point in the history
  • Loading branch information
volkanunsal committed May 7, 2019
1 parent 662593d commit 2017a66
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
52 changes: 52 additions & 0 deletions postgres-rrule.sql
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,49 @@ BEGIN
RETURN result;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION _rrule.rrule_to_jsonb("input" _rrule.RRULE)
RETURNS jsonb AS $$
BEGIN
RETURN jsonb_build_object(
'freq', "input"."freq",
'interval', "input"."interval",
'count', "input"."count",
'until', "input"."until",
'bysecond', "input"."bysecond",
'byminute', "input"."byminute",
'byhour', "input"."byhour",
'byday', "input"."byday",
'bymonthday', "input"."bymonthday",
'byyearday', "input"."byyearday",
'byweekno', "input"."byweekno",
'bymonth', "input"."bymonth",
'bysetpos', "input"."bysetpos",
'wkst', "input"."wkst"
);
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION _rrule.rruleset_to_jsonb("input" _rrule.RRULESET)
RETURNS jsonb AS $$
DECLARE
rrule jsonb;
exrule jsonb;
BEGIN
SELECT _rrule.rrule_to_jsonb("input"."rrule")
INTO rrule;

SELECT _rrule.rrule_to_jsonb("input"."exrule")
INTO exrule;

RETURN jsonb_build_object(
'dtstart', "input"."dtstart",
'dtend', "input"."dtend",
'rrule', rrule,
'exrule', exrule,
'rdate', "input"."rdate",
'exdate', "input"."exdate"
);
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;
CREATE OPERATOR = (
LEFTARG = _rrule.RRULE,
RIGHTARG = _rrule.RRULE,
Expand Down Expand Up @@ -805,3 +848,12 @@ CREATE CAST (jsonb AS _rrule.RRULE)
WITH FUNCTION _rrule.jsonb_to_rrule(jsonb)
AS IMPLICIT;


CREATE CAST (_rrule.RRULE AS jsonb)
WITH FUNCTION _rrule.rrule_to_jsonb(_rrule.RRULE)
AS IMPLICIT;


-- CREATE CAST (_rrule.RRULESET AS jsonb)
-- WITH FUNCTION _rrule.rruleset_to_jsonb(_rrule.RRULESET)
-- AS IMPLICIT;
9 changes: 9 additions & 0 deletions src/casts/casts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ CREATE CAST (jsonb AS _rrule.RRULE)
WITH FUNCTION _rrule.jsonb_to_rrule(jsonb)
AS IMPLICIT;


CREATE CAST (_rrule.RRULE AS jsonb)
WITH FUNCTION _rrule.rrule_to_jsonb(_rrule.RRULE)
AS IMPLICIT;


-- CREATE CAST (_rrule.RRULESET AS jsonb)
-- WITH FUNCTION _rrule.rruleset_to_jsonb(_rrule.RRULESET)
-- AS IMPLICIT;
21 changes: 21 additions & 0 deletions src/functions/0225-rrule_to_jsonb.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CREATE OR REPLACE FUNCTION _rrule.rrule_to_jsonb("input" _rrule.RRULE)
RETURNS jsonb AS $$
BEGIN
RETURN jsonb_build_object(
'freq', "input"."freq",
'interval', "input"."interval",
'count', "input"."count",
'until', "input"."until",
'bysecond', "input"."bysecond",
'byminute', "input"."byminute",
'byhour', "input"."byhour",
'byday', "input"."byday",
'bymonthday', "input"."bymonthday",
'byyearday', "input"."byyearday",
'byweekno', "input"."byweekno",
'bymonth', "input"."bymonth",
'bysetpos', "input"."bysetpos",
'wkst', "input"."wkst"
);
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;
22 changes: 22 additions & 0 deletions src/functions/0230-rruleset_to_jsonb.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CREATE OR REPLACE FUNCTION _rrule.rruleset_to_jsonb("input" _rrule.RRULESET)
RETURNS jsonb AS $$
DECLARE
rrule jsonb;
exrule jsonb;
BEGIN
SELECT _rrule.rrule_to_jsonb("input"."rrule")
INTO rrule;

SELECT _rrule.rrule_to_jsonb("input"."exrule")
INTO exrule;

RETURN jsonb_build_object(
'dtstart', "input"."dtstart",
'dtend', "input"."dtend",
'rrule', rrule,
'exrule', exrule,
'rdate', "input"."rdate",
'exdate', "input"."exdate"
);
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;
18 changes: 17 additions & 1 deletion tests/test_casts.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BEGIN;

SELECT plan(2);
SELECT plan(4);

SET search_path TO public, _rrule;

Expand All @@ -12,6 +12,22 @@ SELECT is(
'when jsonb is cast to RRULESET.'
);

SELECT is(
_rrule.rruleset_to_jsonb('{"dtstart": "19970902T090000", "dtend": "19970903T090000", "rrule": {"freq": "WEEKLY", "count": 4}}'::text::jsonb::_rrule.RRULESET),
$$
{"dtend": "1997-09-03T09:00:00", "rdate": null, "rrule": {"freq": "WEEKLY", "wkst": "MO", "byday": null, "count": 4, "until": null, "byhour": null, "bymonth": null, "byminute": null, "bysecond": null, "bysetpos": null, "byweekno": null, "interval": 1, "byyearday": null, "bymonthday": null}, "exdate": null, "exrule": {"freq": null, "wkst": null, "byday": null, "count": null, "until": null, "byhour": null, "bymonth": null, "byminute": null, "bysecond": null, "bysetpos": null, "byweekno": null, "interval": null, "byyearday": null, "bymonthday": null}, "dtstart": "1997-09-02T09:00:00"}
$$,
'when RRULESET is cast to jsonb.'
);

SELECT is(
'{"freq": "WEEKLY", "count": 4}'::text::jsonb::_rrule.RRULE::jsonb,
$$
{"freq": "WEEKLY", "wkst": "MO", "byday": null, "count": 4, "until": null, "byhour": null, "bymonth": null, "byminute": null, "bysecond": null, "bysetpos": null, "byweekno": null, "interval": 1, "byyearday": null, "bymonthday": null}
$$,
'when RRULE is cast to jsonb.'
);

SELECT is(
'{"freq": "WEEKLY", "count": 4}'::text::jsonb::_rrule.RRULE,
$$
Expand Down

0 comments on commit 2017a66

Please sign in to comment.