~alva/zig-bare

62a89f206a577e95bd64f524325d3819d1701da8 — owl 11 months ago ed630f1
add examples from go-bare
M src/bare.zig => src/bare.zig +4 -8
@@ 38,7 38,7 @@ pub fn Decoder(comptime ReaderType: type) type {
        }

        /// Decode a supported BARE type.
        pub fn decode(self: *Self, comptime T: type) !ReturnType(T) {
        pub fn decode(self: *Self, comptime T: type) !T {
            return switch (@typeInfo(T)) {
                .Int => self.decodeInt(T),
                .Float => self.decodeFloat(T),


@@ 177,7 177,7 @@ pub fn Decoder(comptime ReaderType: type) type {
            return buf;
        }

        fn decodeHashMap(self: *Self, comptime T: type) !T.Unmanaged {
        fn decodeHashMap(self: *Self, comptime T: type) !T {
            const K = HashMapKeyType(T);
            const V = HashMapValueType(T);



@@ 189,8 189,8 @@ pub fn Decoder(comptime ReaderType: type) type {
            if (std.math.maxInt(T.Size) < i)
                return DecodeError.Overflow;

            var map = T.Unmanaged{};
            try map.ensureUnusedCapacity(self.arena.allocator(), @intCast(i));
            var map = T.init(self.arena.allocator());
            try map.ensureUnusedCapacity(@intCast(i));

            for (0..i) |_| {
                const key = try self.decode(K);


@@ 206,10 206,6 @@ pub fn Decoder(comptime ReaderType: type) type {

            return map;
        }

        fn ReturnType(comptime T: type) type {
            return if (isHashMap(T)) T.Unmanaged else T;
        }
    };
}


A src/interop/go-bare/examples/customer.bin => src/interop/go-bare/examples/customer.bin +0 -0
A src/interop/go-bare/examples/employee.bin => src/interop/go-bare/examples/employee.bin +0 -0
A src/interop/go-bare/examples/people.bin => src/interop/go-bare/examples/people.bin +0 -0
A src/interop/go-bare/examples/schema.zig => src/interop/go-bare/examples/schema.zig +138 -0
@@ 0,0 1,138 @@
const std = @import("std");
const bare = @import("bare");

const string = []const u8;
const MetaData = std.StringHashMap([]const u8);

const PublicKey = [128]u8;
const Time = string; // ISO 8601 string

const Department = enum(usize) {
    ACCOUNTING,
    ADMINISTRATION,
    CUSTOMER_SERVICE,
    DEVELOPMENT,

    // Reserved for the CEO
    JSMITH = 99,
};

const Customer = struct {
    name: string,
    email: string,
    address: Address,
    orders: []struct {
        orderId: i64,
        quantity: i32,
    },
    metadata: MetaData,
};

const Employee = struct {
    name: string,
    email: string,
    address: Address,
    department: Department,
    hireDate: Time,
    publicKey: ?PublicKey,
    metadata: MetaData,
};

const TerminatedEmployee = void;

const Person = union(enum) {
    customer: Customer,
    employee: Employee,
    terminated_employee: TerminatedEmployee,
};

const Address = struct {
    address: [4]string,
    city: string,
    state: string,
    country: string,
};

const testing = std.testing;

test "decode customer.bin" {
    const customer = @embedFile("customer.bin");
    var fbs = std.io.fixedBufferStream(customer);

    const reader = fbs.reader();
    var d = bare.decoder(testing.allocator, reader);
    defer d.deinit();
    const res = try d.decode(Person);

    switch (res) {
        .customer => |c| {
            try testing.expectEqualSlices(u8, "James Smith", c.name);
            try testing.expectEqualSlices(u8, "jsmith@example.org", c.email);
        },
        else => unreachable,
    }
}

test "decode employee.bin" {
    const customer = @embedFile("employee.bin");
    var fbs = std.io.fixedBufferStream(customer);

    const reader = fbs.reader();
    var d = bare.decoder(testing.allocator, reader);
    defer d.deinit();
    const res = try d.decode(Person);

    switch (res) {
        .employee => |c| {
            try testing.expectEqualSlices(u8, "Tiffany Doe", c.name);
            try testing.expectEqualSlices(u8, "tiffanyd@acme.corp", c.email);
        },
        else => unreachable,
    }
}

test "decode terminated.bin" {
    const customer = @embedFile("terminated.bin");
    var fbs = std.io.fixedBufferStream(customer);

    const reader = fbs.reader();
    var d = bare.decoder(testing.allocator, reader);
    defer d.deinit();
    const res = try d.decode(Person);

    switch (res) {
        .terminated_employee => |t| {
            try testing.expectEqual(t, {});
        },
        else => unreachable,
    }
}

test "decode people.bin" {
    const customer = @embedFile("people.bin");
    var fbs = std.io.fixedBufferStream(customer);

    const reader = fbs.reader();
    var d = bare.decoder(testing.allocator, reader);
    defer d.deinit();
    var res = try d.decode(Person);

    switch (res) {
        .customer => {},
        else => unreachable,
    }

    res = try d.decode(Person);

    switch (res) {
        .employee => {},
        else => unreachable,
    }

    res = try d.decode(Person);

    switch (res) {
        .terminated_employee => {},
        else => unreachable,
    }
}

A src/interop/go-bare/examples/terminated.bin => src/interop/go-bare/examples/terminated.bin +1 -0
@@ 0,0 1,1 @@

\ No newline at end of file

M src/test.zig => src/test.zig +1 -0
@@ 17,6 17,7 @@ test "refAllDecls" {
    std.testing.refAllDecls(bare);
    std.testing.refAllDecls(@import("interop/go-bare/unmarshal_test.zig"));
    std.testing.refAllDecls(@import("interop/go-bare/marshal_test.zig"));
    std.testing.refAllDecls(@import("interop/go-bare/examples/schema.zig"));
}

test "read u8" {