cheddar
·
2025-04-18
2024-01-09-01-Building_A_Proof_of_Concept.md
1---
2date: 2024-01-09
3title: Building a Proof of Concept
4series:
5 name: "SDBD: Creating a Data Format"
6 number: 4
7---
8The format certainly looks sound. Could there be any surprises when we try to implement it? There's one way to find out. I'll write a Proof of Concept. Finally, we get to the code!
9## Looking Sharp
10My career experience has mostly been with .NET and web development. My latest job had me coding in Ruby on Rails. After a two year absence, I'm anxious to get back into the world of .NET. I'm going to do the implementation in C# with .NET 8.0.
11## Start at the interface
12When I code, I always start at the interfaces. Whether it's the user interface (UI) or the application programming interfaces (APIs), starting at the interfaces sets up good boundaries that guide good design. I'll start by defining the contract for an API that encodes and decodes SDBD data.
13```cs
14namespace SDBD;
15
16public interface ICodec {
17 Document Decode(byte[] data);
18 byte[] Encode(Document document);
19}
20
21public record Document(
22 Dictionary<string, string> Metadata,
23 byte[] Data
24);
25```
26## A basic demo
27The demo program will be a command line encoder/decoder. To encode, pass a file path on its own or with `-e` as the first parameter. The program will encode the file to SDBD with the original filename embedded and write a file with a `.sdbd` extension. To decode, pass `-d` as the first parameter and the path to an `.sdbd` file. It will write a file with the original data and the original filename.
28```cs
29SDBD.ICodec codec = new SDBD.Codec();
30
31var (encode, filepath) = ParseArgs(args);
32var inputData = File.ReadAllBytes(filepath);
33var filename = Path.GetFileName(filepath);
34
35if(encode) {
36 SDBD.Document document = new (
37 new() {
38 { "content-name", filename }
39 },
40 inputData
41 );
42
43 var outputData = codec.Encode(document);
44
45 File.WriteAllBytes($"{filename}.sdbd", outputData);
46} else {
47 var document = codec.Decode(inputData);
48
49 File.WriteAllBytes(document.Metadata["content-name"], document.Data);
50}
51
52(bool encode, string filepath) ParseArgs(string[] args) {
53 return args switch {
54 [var filepath] => (true, filepath),
55 ["-d", var filepath] => (false, filepath),
56 ["-e", var filepath] => (true, filepath),
57 _ => throw new Exception("I don't like those arguments")
58 };
59}
60```
61## First run
62I'll mock up an implementation of the interface to make sure the program is working. All it will do is echo back the data it's given, and give the document the name `text.txt`.
63```cs
64namespace SDBD;
65
66public class Codec : ICodec {
67 public Document Decode(byte[] data) {
68 return new(
69 new () {
70 { "content-name", "text.txt" }
71 },
72 data
73 );
74 }
75
76 public byte[] Encode(Document document) {
77 return document.Data;
78 }
79}
80```
81
82If we run the program on any file, it will output the same file with the `.sdbd` extension added. Run the program on a file with the `-d` parameter and it will output the same data with the file name `text.txt`. Looking good so far.