Commit 4b417257 authored by Carlos Davide's avatar Carlos Davide

day 22

parent b2f57d04
......@@ -34,3 +34,5 @@ Index:
- **day 19**: Object Oriented, established internet company version.
- **day 20**: Object Oriented, fully OOssified.
- **day 21**: Generalized FizzBuzz DSL.
- **day 22**: SmarTazz: All-in-one, XML configurable, abstract enterprise rules
engine for on-prem FizzBuzzBazzing.
......@@ -2,6 +2,7 @@ package day00_test
import (
"fmt"
"os"
"testing"
// we'll add the implementations here...
......@@ -26,12 +27,15 @@ import (
"gitlab.com/carlosdavidepto/cavalcade-2018/day19"
"gitlab.com/carlosdavidepto/cavalcade-2018/day20"
"gitlab.com/carlosdavidepto/cavalcade-2018/day21"
// "gitlab.com/carlosdavidepto/cavalcade-2018/day22"
"gitlab.com/carlosdavidepto/cavalcade-2018/day22"
// "gitlab.com/carlosdavidepto/cavalcade-2018/day23"
// "gitlab.com/carlosdavidepto/cavalcade-2018/day24"
)
func TestAllImplementations(t *testing.T) {
// set config path for day22, CWD is day00 as per package `testing` doc
_ = os.Setenv(day22.CONFIG_PATH_ENV_VAR, "../day22/config.xml")
implementations := []func(int) string{
// ... and here.
day01.FizzBuzz,
......@@ -55,7 +59,7 @@ func TestAllImplementations(t *testing.T) {
day19.FizzBuzz,
day20.FizzBuzz,
day21.FizzBuzz,
// day22.FizzBuzz,
day22.MakeXMLRulesApplicator(), // xD hilarious.
// day23.FizzBuzz,
// day24.FizzBuzz,
}
......
<?xml version="1.0" encoding="UTF-8"?>
<config>
<rules>
<rule divisor="3" part="fizz" />
<rule divisor="5" part="buzz" />
</rules>
</config>
package day22
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
const CONFIG_PATH_ENV_VAR = "DAY22_CONFIG_PATH"
// FizzBuzz needs to be lazily initialized due to the use of `os.Env`.
// See test harness.
// var FizzBuzz func(n int) string = MakeXMLRulesApplicator()
func MakeXMLRulesApplicator() func(int) string {
path := os.Getenv(CONFIG_PATH_ENV_VAR)
config := LoadConfig(LoadConfigFileBytes(path))
rules := config.Rules
return func(n int) string {
s := ""
for _, r := range rules {
if n%r.Divisor == 0 {
s = s + r.Part
}
}
if s == "" {
return fmt.Sprint(n)
}
return s
}
}
func LoadConfig(configBytes []byte) Config {
config := Config{}
err := xml.Unmarshal(configBytes, &config)
if err != nil {
panic(err)
}
return config
}
func LoadConfigFileBytes(path string) []byte {
configBytes, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
return configBytes
}
type Config struct {
XMLName xml.Name `xml:"config"`
Rules []Rule `xml:"rules>rule"`
}
type Rule struct {
Divisor int `xml:"divisor,attr"`
Part string `xml:"part,attr"`
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment