Skip to content

Instantly share code, notes, and snippets.

@vendelin8
Last active June 1, 2021 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vendelin8/be56b25cf60c06e3b090c2483988e73c to your computer and use it in GitHub Desktop.
Save vendelin8/be56b25cf60c06e3b090c2483988e73c to your computer and use it in GitHub Desktop.
// Code generated by colf(1); DO NOT EDIT.
// The compiler used schema file demo.colf for package demo.
/// Package demo offers a demonstration.
/// These comment lines will end up in the generated code.
import 'dart:convert';
import 'dart:typed_data';
import 'package:collection/collection.dart';
import 'package:quiver/core.dart';
/// The upper limit for serial byte sizes.
const colferSizeMax = 16 * 1024 * 1024;
/// The upper limit for the number of elements in a list.
const colferListMax = 64 * 1024;
/// Course is the grounds where the game of golf is played.
class Course {
Course({
this.iD = 0,
this.name = '',
List<Hole>? holes,
Uint8List? image,
List<String>? tags,
}) : holes = holes ?? [],
image = image ?? Uint8List(0),
tags = tags ?? [];
int iD;
String name;
List<Hole?> holes;
Uint8List image;
List<String> tags;
@override
bool operator ==(Object other) =>
(other is Course) &&
other.iD == iD &&
other.name == name &&
const IterableEquality().equals(other.holes, holes) &&
const IterableEquality().equals(other.image, image) &&
const IterableEquality().equals(other.tags, tags);
@override
String toString() => 'class Course {'
'iD: ${iD.toString()}'
', name: "$name"'
', holes: List<Hole>${holes.toString()}'
', image: ${image.toString()}'
', tags: [${tags.isNotEmpty ? "\"tags.join('\", \"')}" : ""}]';
@override
int get hashCode => hashObjects([iD, name, holes, image, tags]);
/// Returns an over estimatation of marshal length.
///
/// Throws [RangeError] if the size of a list exceeds [colferListMax].
/// Returns an over estimated length for the required buffer. String
/// characters are counted for 4 bytes, everything has its exact size.
int marshalLen() {
int _l = 1;
{
int _x = iD;
if (_x < 0 || 0x2000000000000 <= _x) {
_l += 9;
} else if (_x != 0) {
for (_l += 2; _x >= 0x80; _l++) {
_x >>= 7;
}
}
}
{
int _x = name.length;
if (_x != 0) {
_x *= 4;
for (_l += _x + 2; _x >= 0x80; _l++) {
_x >>= 7;
}
}
}
{
int _x = holes.length;
if (_x != 0) {
if (_x > colferListMax) {
throw RangeError.range(_x, null, colferListMax, 'demo.course.holes', 'colfer');
}
for (_l += 2; _x >= 0x80; _l++) {
_x >>= 7;
}
for (final _v in holes) {
if (_v == null) {
_l++;
continue;
}
_l += _v.marshalLen();
}
}
}
{
int _x = image.length;
if (_x != 0) {
for (_l += _x + 2; _x >= 0x80; _l++) {
_x >>= 7;
}
}
}
{
int _x = tags.length;
if (_x != 0) {
if (_x > colferListMax) {
throw RangeError.range(_x, null, colferListMax, 'demo.course.tags', 'colfer');
}
for (_l += 2; _x >= 0x80; _l++) {
_x >>= 7;
}
for (final _a in tags) {
_x = _a.length;
_x *= 4;
for (_l += _x + 1; _x >= 0x80; _l++) {
_x >>= 7;
}
}
}
}
if (_l > colferSizeMax) {
return colferSizeMax;
}
return _l;
}
/// Encodes as Colfer into [_buf].
///
/// Throws [RangeError] if uint8, uint16, uint32 or int32 value overflows or
/// underflows, or when the size of a list exceeds [colferListMax], or if a
/// text, binary, or [_buf] exceeds [colferSizeMax]. Returns the number of
/// bytes written.
int marshalTo(Uint8List _buf) {
var _view = _buf.buffer.asByteData();
int _i = 0;
{
int _x = iD;
if (_x != 0) {
if (0 < _x && _x < 0x2000000000000) {
_buf[_i] = 0;
_i++;
while (_x > 127) {
_buf[_i] = (_x & 127) | 128;
_i++;
_x >>= 7;
}
_buf[_i] = _x;
_i++;
} else {
_buf[_i] = 0 | 128;
_view.setInt64(_i + 1, _x);
_i += 9;
}
}
}
{
int _x = name.length;
if (_x > 0) {
_buf[_i] = 1;
_i++;
var _v = utf8.encode(name);
_x = _v.length;
while (_x > 127) {
_buf[_i] = (_x & 127) | 128;
_i++;
_x >>= 7;
}
_buf[_i] = _x;
_buf.setAll(_i + 1, _v);
_i += 1 + _v.length;
}
}
{
int _x = holes.length;
if (_x > 0) {
if (_x > colferListMax) {
throw RangeError.range(_x, null, colferListMax, 'demo.course.holes', 'colfer');
}
_buf[_i] = 2;
_i++;
while (_x > 127) {
_buf[_i] = (_x & 127) | 128;
_i++;
_x >>= 7;
}
_buf[_i] = _x;
_i++;
for (var _vi in holes) {
_vi ??= Hole();
_i += _vi.marshalTo(Uint8List.view(_buf.buffer, _i));
}
}
}
{
int _x = image.length;
if (_x > 0) {
_buf[_i] = 3;
_i++;
var _v = image;
_x = _v.length;
while (_x > 127) {
_buf[_i] = (_x & 127) | 128;
_i++;
_x >>= 7;
}
_buf[_i] = _x;
_buf.setAll(_i + 1, _v);
_i += 1 + _v.length;
}
}
{
int _x = tags.length;
if (_x > 0) {
if (_x > colferListMax) {
throw RangeError.range(_x, null, colferListMax, 'demo.course.tags', 'colfer');
}
_buf[_i] = 4;
_i++;
while (_x > 127) {
_buf[_i] = (_x & 127) | 128;
_i++;
_x >>= 7;
}
_buf[_i] = _x;
_i++;
for (final _vi in tags) {
var _v = utf8.encode(_vi);
_x = _v.length;
while (_x > 127) {
_buf[_i] = (_x & 127) | 128;
_i++;
_x >>= 7;
}
_buf[_i] = _x;
_buf.setAll(_i + 1, _v);
_i += 1 + _v.length;
}
}
}
_buf[_i] = 127;
_i++;
if (_i > colferSizeMax) {
throw RangeError.range(_i, null, colferSizeMax, 'demo.course', 'colfer');
}
return _i;
}
/// Decodes [_data] as Colfer.
///
/// Throws [RangeError] if there is an unexpexted end of data, if a list
/// exceeds [colferListMax], or if a text, binary or [_data] exceeds
/// [colferSizeMax]. Throws [StateError] if ending header mismatches.
/// Returns the number of bytes read.
int unmarshal(Uint8List _data) {
int _header = 0;
int _i = 0;
var _view = ByteData.view(_data.buffer);
_header = _data[_i];
_i++;
if (_header == 0) {
int _c = _data[_i];
_i++;
if (_c >= 0x80) {
_c &= 0x7f;
for (int _shift = 7;; _shift += 7) {
int _b = _data[_i];
_i++;
if (_b < 0x80 || _shift == 56) {
_c |= _b << _shift;
break;
}
_c |= (_b & 0x7f) << _shift;
}
}
iD = _c;
_header = _data[_i];
_i++;
} else if (_header == (0 | 128)) {
int _v = _view.getUint32(_i) * 0x100000000;
_v += _view.getUint32(_i + 4);
iD = _v;
_header = _data[_i + 8];
_i += 9;
}
if (_header == 1) {
int _size = _data[_i];
_i++;
if (_size >= 0x80) {
_size &= 0x7f;
for (int _shift = 7;; _shift += 7) {
int _b = _data[_i];
_i++;
if (_b < 0x80 || _shift == 56) {
_size |= _b << _shift;
break;
}
_size |= (_b & 0x7f) << _shift;
}
}
if (_size < 0 || _size > colferSizeMax) {
throw RangeError.range(_size, 0, colferSizeMax, 'demo.course.name', 'colfer');
}
int _s = _i;
_i += _size;
name = utf8.decode(_data.sublist(_s, _i));
_header = _data[_i];
_i++;
}
if (_header == 2) {
int _c = _data[_i];
_i++;
if (_c >= 0x80) {
_c &= 0x7f;
for (int _shift = 7;; _shift += 7) {
int _b = _data[_i];
_i++;
if (_b < 0x80 || _shift == 56) {
_c |= _b << _shift;
break;
}
_c |= (_b & 0x7f) << _shift;
}
}
if (_c < 0 || _c > colferListMax) {
throw RangeError.range(_c, 0, colferListMax, 'demo.course.holes', 'colfer');
}
if (holes.length != _c) {
holes = List<Hole>.filled(_c, Hole());
}
for (var _ci in holes) {
_ci ??= Hole();
_i += _ci.unmarshal(_data.sublist(_i));
}
_header = _data[_i];
_i++;
}
if (_header == 3) {
int _size = _data[_i];
_i++;
if (_size >= 0x80) {
_size &= 0x7f;
for (int _shift = 7;; _shift += 7) {
int _b = _data[_i];
_i++;
if (_b < 0x80 || _shift == 56) {
_size |= _b << _shift;
break;
}
_size |= (_b & 0x7f) << _shift;
}
}
if (_size < 0 || _size > colferSizeMax) {
throw RangeError.range(_size, 0, colferSizeMax, 'demo.course.image', 'colfer');
}
int _start = _i;
_i += _size;
image = _data.sublist(_start, _i);
_header = _data[_i];
_i++;
}
if (_header == 4) {
int _c = _data[_i];
_i++;
if (_c >= 0x80) {
_c &= 0x7f;
for (int _shift = 7;; _shift += 7) {
int _b = _data[_i];
_i++;
if (_b < 0x80 || _shift == 56) {
_c |= _b << _shift;
break;
}
_c |= (_b & 0x7f) << _shift;
}
}
if (_c < 0 || _c > colferListMax) {
throw RangeError.range(_c, 0, colferListMax, 'demo.course.tags', 'colfer');
}
if (tags.length != _c) {
tags = List<String>.filled(_c, '');
}
for (int _ci = 0; _ci < _c; _ci++) {
int _size = _data[_i];
_i++;
if (_size >= 0x80) {
_size &= 0x7f;
for (int _shift = 7;; _shift += 7) {
int _b = _data[_i];
_i++;
if (_b < 0x80 || _shift == 56) {
_size |= _b << _shift;
break;
}
_size |= (_b & 0x7f) << _shift;
}
}
if (_size < 0 || _size > colferSizeMax) {
throw RangeError.range(_size, 0, colferSizeMax, 'demo.course.tags', 'colfer');
}
int _s = _i;
_i += _size;
tags[_ci] = utf8.decode(_data.sublist(_s, _i));
}
_header = _data[_i];
_i++;
}
if (_header != 127) {
throw StateError('colfer: unknown header $_header at byte ${_i - 1}');
}
if (_i > colferSizeMax) {
throw RangeError.range(_i, null, colferSizeMax, 'demo.course', 'colfer');
}
return _i;
}
}
class Hole {
Hole({
this.lat = 0.0,
this.lon = 0.0,
this.par = 0,
this.water = false,
this.sand = false,
});
/// Lat is the latitude of the cup.
double lat;
/// Lon is the longitude of the cup.
double lon;
/// Par is the difficulty index.
int par;
/// Water marks the presence of water.
bool water;
/// Sand marks the presence of sand.
bool sand;
@override
bool operator ==(Object other) =>
(other is Hole) &&
other.lat == lat &&
other.lon == lon &&
other.par == par &&
other.water == water &&
other.sand == sand;
@override
String toString() => 'class Hole {'
'lat: ${lat.toString()}'
', lon: ${lon.toString()}'
', par: ${par.toString()}'
', water: ${water.toString()}'
', sand: ${sand.toString()}';
@override
int get hashCode => hashObjects([lat, lon, par, water, sand]);
/// Returns an over estimatation of marshal length.
///
/// Throws [RangeError] if the size of a list exceeds [colferListMax].
/// Returns an over estimated length for the required buffer. String
/// characters are counted for 4 bytes, everything has its exact size.
int marshalLen() {
int _l = 1;
{
if (lat != 0) {
_l += 9;
}
}
{
if (lon != 0) {
_l += 9;
}
}
{
if (par != 0) {
_l += 2;
}
}
{
if (water) {
_l++;
}
}
{
if (sand) {
_l++;
}
}
if (_l > colferSizeMax) {
return colferSizeMax;
}
return _l;
}
/// Encodes as Colfer into [_buf].
///
/// Throws [RangeError] if uint8, uint16, uint32 or int32 value overflows or
/// underflows, or when the size of a list exceeds [colferListMax], or if a
/// text, binary, or [_buf] exceeds [colferSizeMax]. Returns the number of
/// bytes written.
int marshalTo(Uint8List _buf) {
var _view = _buf.buffer.asByteData();
int _i = 0;
{
if (lat != 0) {
_buf[_i] = 0;
if (lat.isNaN) {
_buf[_i + 1] = 0x7f;
_buf[_i + 2] = 0xf8;
_buf[_i + 8] = 1;
} else {
_view.setFloat64(_i + 1, lat);
}
_i += 9;
}
}
{
if (lon != 0) {
_buf[_i] = 1;
if (lon.isNaN) {
_buf[_i + 1] = 0x7f;
_buf[_i + 2] = 0xf8;
_buf[_i + 8] = 1;
} else {
_view.setFloat64(_i + 1, lon);
}
_i += 9;
}
}
{
if (par != 0) {
if (par > 255 || par < 0) {
throw RangeError.range(par, 0, 255, 'demo.hole.par', 'colfer');
}
_buf[_i] = 2;
_buf[_i + 1] = par;
_i += 2;
}
}
{
if (water) {
_buf[_i] = 3;
_i++;
}
}
{
if (sand) {
_buf[_i] = 4;
_i++;
}
}
_buf[_i] = 127;
_i++;
if (_i > colferSizeMax) {
throw RangeError.range(_i, null, colferSizeMax, 'demo.hole', 'colfer');
}
return _i;
}
/// Decodes [_data] as Colfer.
///
/// Throws [RangeError] if there is an unexpexted end of data, if a list
/// exceeds [colferListMax], or if a text, binary or [_data] exceeds
/// [colferSizeMax]. Throws [StateError] if ending header mismatches.
/// Returns the number of bytes read.
int unmarshal(Uint8List _data) {
int _header = 0;
int _i = 0;
var _view = ByteData.view(_data.buffer);
_header = _data[_i];
_i++;
if (_header == 0) {
lat = _view.getFloat64(_i);
_i += 8;
_header = _data[_i];
_i++;
}
if (_header == 1) {
lon = _view.getFloat64(_i);
_i += 8;
_header = _data[_i];
_i++;
}
if (_header == 2) {
par = _data[_i];
_header = _data[_i + 1];
_i += 2;
}
if (_header == 3) {
water = true;
_header = _data[_i];
_i++;
}
if (_header == 4) {
sand = true;
_header = _data[_i];
_i++;
}
if (_header != 127) {
throw StateError('colfer: unknown header $_header at byte ${_i - 1}');
}
if (_i > colferSizeMax) {
throw RangeError.range(_i, null, colferSizeMax, 'demo.hole', 'colfer');
}
return _i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment