Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cht-compile can now emit .schema.json files.

  • Loading branch information...
commit 4ca1a0aa8c2ef0f76fece99614b01b9f47c3077d 1 parent be850ef
@agentzh authored
Showing with 63 additions and 8 deletions.
  1. +18 −8 bin/cht-compile
  2. +45 −0 lib/Cheater/Eval.pm
View
26 bin/cht-compile
@@ -57,19 +57,29 @@ if (! -d $outdir) {
}
for my $table (sort keys %$computed) {
- my $data = $eval->canonicalize_table($table, $computed->{$table});
+ {
+ my $schema = $eval->gen_table_schema($table);
- my @rows;
- for my $row (@$data) {
- push @rows, $json_xs->encode($row);
+ my $json = $json_xs->encode($schema);
+
+ my $outfile = "$outdir/$table.schema.json";
+ write_file($outfile, $json);
+ warn "Wrote $outfile\n";
}
- my $outfile = "$outdir/$table.rows.json";
+ {
+ my $data = $eval->canonicalize_table($table, $computed->{$table});
- my $json = "[" . join(",\n", @rows) . "]";
+ my @rows;
+ for my $row (@$data) {
+ push @rows, $json_xs->encode($row);
+ }
- write_file($outfile, $json);
+ my $json = "[" . join(",\n", @rows) . "]";
- warn "Wrote $outfile\n";
+ my $outfile = "$outdir/$table.rows.json";
+ write_file($outfile, $json);
+ warn "Wrote $outfile\n";
+ }
}
View
45 lib/Cheater/Eval.pm
@@ -677,4 +677,49 @@ sub canonicalize_table {
return \@rows;
}
+sub gen_table_schema {
+ my ($self, $table) = @_;
+
+ my $ast = $self->ast;
+ my $deps = $ast->deps;
+ my $tables = $ast->tables;
+ my $cols = $ast->cols;
+
+ my $tb_spec = $tables->{$table} or
+ die "Table $table not defined.\n";
+
+ my @col_defs;
+
+ for my $col (@$tb_spec) {
+ use Data::Dumper;
+
+ my $name = $col->[0];
+ my $qcol = "$table.$name";
+ my $spec = $cols->{$qcol};
+ my $type = $spec->{type};
+
+ while ($type eq 'refs') {
+ my $dep = $deps->{$qcol};
+
+ my $col = $cols->{$dep};
+ $type = $col->{type};
+ }
+
+ #say "type: $type";
+ #say Dumper($spec);
+ my @attrs = grep { $_ =~ /^(?:not null|unique)$/ } @{ $spec->{attrs} };
+
+ push @col_defs, {
+ name => $name,
+ type => $type,
+ attrs => \@attrs,
+ }
+ }
+
+ return {
+ name => $table,
+ cols => \@col_defs,
+ };
+}
+
1;

0 comments on commit 4ca1a0a

Please sign in to comment.
Something went wrong with that request. Please try again.