Up to Main Index Up to Journal for January, 2014 JOURNAL FOR FRIDAY 17TH JANUARY, 2014 ______________________________________________________________________________ SUBJECT: Decoders and Encoders DATE: Fri 17 Jan 23:17:34 GMT 2014 What a busy old week it's been. Ended up working late on Wednesday so WolfMUD Wednesday was bumped to Thursday instead. While working on the account creation code I realise I still hadn't written the missing half of recordjar. We have a decoder but not an encoder - something I'd been ignoring until now. The recordjar decoder was written quite early on and I knew I hadn't got it right even then. Which is probably why the opposing half was never done. Before I could start work on the encoder the decoder obviously needed sorting out first. While going over the decoder code and trying to decide on the best way to improve it I found some extreme ugliness in the basic.Init() code. So I became a little side tracked while I sorted that out. The result is a new helper in the decoder called decoder.PairList, this little helper will take a whitespace separated list of pairs - which are delimited by any non-letter or non-digit. For example exits are defined as: Exits: E→L3 SE→L4 S→L2 PairList will return a slice of 2 element string arrays, [][2]string. If there is more than one non-letter or non-digit the pair will only be split on the first of them. In the above example we would get a slice of 3 arrays: [][2]string{ {"E","L3"}, {"SE","L4"}, {"S","L2"}, } Easy to range over and simplified basic.Init() quite a bit. After that distraction I went back to going over the decoder code. Currently the decoder helpers take a Record which is not good. For example to decode a list of pairs we have: recordjar.Record.PairList() Now we also want that for encoding as well! What I have decided to do is define Decoder and Encoder as an alias of Record. Now we can have: recordjar.Decoder.PairList(property string) [][2]string recordjar.Encoder.PairList(property string, [][2]string) Which makes things nice and symmetrical. Also you don't have to use the helpers, you can still access properties directly in the record slice if you want to. For example: d.PairList("exits") // Using decoder helper d["exits"] // Direct raw string So now I'm just switching the unmarshalers over to using decoders and then I can write the marshalers which will use the encoders. At that point we should be able to load and save things. This includes saving a player with all their gear as well - the recursiveness for saving will just be there the same as loading is now ;) -- Diddymus Up to Main Index Up to Journal for January, 2014