Skip to content

Commit

Permalink
ELEC-573: Add support for Golang Templates (#32)
Browse files Browse the repository at this point in the history
This configures codegen-tooling to support Golang and build Golang templates.

This is to support faking of data in Go to aid in website development.
  • Loading branch information
ckitagawa authored and karlding committed Feb 28, 2019
1 parent 2c539b3 commit f3c11e2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ branches:

before_install:
- sudo apt-get -qq update
- sudo apt-get install golang

install:
- pip install -r requirements.txt
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ gen: protos
@echo "Generating from templates..."
@python codegen/build.py
@find out -type f \( -iname '*.[ch]' -o -iname '*.ts' \) | xargs -r clang-format -i -fallback-style=Google
@find out -type f \( -iname '*.go' \) | xargs -r gofmt -w

lint:
@echo "Linting..."
Expand Down
21 changes: 21 additions & 0 deletions helpers/helpers.mako
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,24 @@ Args:
% endfor
NUM_${prefix}S = ${len(data)} \
</%def>

<%doc>
Helps generating enum body content in the format:
prefix_val0 type = 0,
prefix_val1 type = 2,
prefix_val2 type = 5,
...
Args:
data: a dictionary of data in the range (empty values should have a value of None)
prefix: the prefix to use
go_type: the type in go
</%doc>
<%def name="generate_enum_go(data, prefix, go_type)"> \
% for key, value in data.items():
% if value != None:
${prefix}${''.join(x.title() for x in value.split('_'))} ${go_type} = ${key}
% endif
% endfor
</%def>
18 changes: 18 additions & 0 deletions templates/can_msg_defs.mako.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%namespace name="helpers" file="/helpers/helpers.mako" /> \
<% from data import NUM_CAN_MESSAGES, NUM_CAN_DEVICES, parse_can_device_enum, parse_can_message_enum, parse_can_frames %> \
package CanMsgDefs

type CanDeviceId uint16
type CanMsgId uint32

// For setting the CAN device
const (
<% can_devices = parse_can_device_enum() %> \
${helpers.generate_enum_go(can_devices, 'SystemCanDevice', 'CanDeviceId')}
)

// For setting the CAN message ID
const (
<% can_messages = parse_can_message_enum(options.filename) %> \
${helpers.generate_enum_go(can_messages, 'SystemCanMessage', 'CanMsgId')}
)

0 comments on commit f3c11e2

Please sign in to comment.