summaryrefslogtreecommitdiffstats
path: root/lib/Lintian/Data/PreambledJSON.pm
blob: e2af970bd1ec9bee62ddbdc930f53dd638e1dc8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- perl -*-

# Copyright (C) 2021 Felix Lechner
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.

package Lintian::Data::PreambledJSON;

use v5.20;
use warnings;
use utf8;

use Carp qw(carp);
use Const::Fast;
use JSON::MaybeXS;
use Path::Tiny;
use Time::Piece;
use Unicode::UTF8 qw(encode_utf8);

use Moo::Role;
use namespace::clean;

const my $EMPTY => q{};

const my $PREAMBLE => q{preamble};
const my $TITLE => q{title};
const my $CARGO => q{cargo};

=encoding utf-8

=head1 NAME

Lintian::Data::PreambledJSON -- Data in preambled JSON format

=head1 SYNOPSIS

 use Lintian::Data::PreambledJSON;

=head1 DESCRIPTION

Routines for access and management of preambled JSON data files.

=head1 INSTANCE METHODS

=over 4

=item last_modified

=cut

has cargo => (
    is => 'rw',
    coerce => sub { my ($scalar) = @_; return ($scalar // $EMPTY); }
);

=item read_file

=cut

sub read_file {
    my ($self, $path, $double_reference) = @_;

    if (!length $path || !-e $path) {

        carp encode_utf8("Unknown data file: $path");
        return 0;
    }

    my $json = path($path)->slurp;
    my $data = decode_json($json);

    my %preamble = %{$data->{$PREAMBLE}};
    my $stored_title = $preamble{$TITLE};
    my $storage_key = $preamble{$CARGO};

    unless (length $stored_title && length $storage_key) {
        warn encode_utf8("Please refresh data file $path: invalid format");
        return 0;
    }

    unless ($stored_title eq $self->title) {
        warn encode_utf8(
            "Please refresh data file $path: wrong title $stored_title");
        return 0;
    }

    if ($storage_key eq $PREAMBLE) {
        warn encode_utf8(
            "Please refresh data file $path: disallowed cargo key $storage_key"
        );
        return 0;
    }

    if (!exists $data->{$storage_key}) {
        warn encode_utf8(
            "Please refresh data file $path: cargo key $storage_key not found"
        );
        return 0;
    }

    ${$double_reference} = $data->{$storage_key};

    return 1;
}

=item write_file

=cut

sub write_file {
    my ($self, $storage_key, $reference, $path) = @_;

    die
"Cannot write preambled JSON data file $path: disallowed cargo key $storage_key"
      if $storage_key eq $PREAMBLE;

    my %preamble;
    $preamble{$TITLE} = $self->title;
    $preamble{$CARGO} = $storage_key;

    my %combined;
    $combined{$PREAMBLE} = \%preamble;
    $combined{$storage_key} = $reference;

    # convert to UTF-8 prior to encoding in JSON
    my $encoder = JSON->new;
    $encoder->canonical;
    $encoder->utf8;
    $encoder->pretty;

    my $json = $encoder->encode(\%combined);

    my $parentdir = path($path)->parent->stringify;
    path($parentdir)->mkpath
      unless -e $parentdir;

    # already in UTF-8
    path($path)->spew($json);

    return 1;
}

=back

=cut

1;

# Local Variables:
# indent-tabs-mode: nil
# cperl-indent-level: 4
# End:
# vim: syntax=perl sw=4 sts=4 sr et