Last active
January 18, 2019 07:57
-
-
Save RohitRox/977f6f66e96e7b20ac81a0a2ade92654 to your computer and use it in GitHub Desktop.
Golang Templating
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"regexp" | |
"strconv" | |
) | |
func main() { | |
template := `{ | |
"ApplicationLogger": { | |
"GlobalLogLevel": ${GlobalLogLevel}, | |
}, | |
"InstrumentationNewRelic": { | |
"Enabled": ${NewRelicEnabled}, | |
"License": ${NewRelicKey} | |
}, | |
"IntegerAttr": ${IntegerAttr}, | |
"MissingAdditionalProp": ${MissingAdditionalProp} | |
}` | |
exp := `\${([a-zA-Z0-9_]+)}` | |
re := regexp.MustCompile(exp) | |
configJSON := `{ | |
"GlobalLogLevel" : "INFO", | |
"NewRelicEnabled" : true, | |
"NewRelicKey" : "xxx-xxx-xxx", | |
"IntegerAttr" : 50, | |
"AdditionalAttr" : "whatever", | |
"AnArray": ["val1", "val2"] | |
}` | |
configs := make(map[string]interface{}) | |
err := json.Unmarshal([]byte(configJSON), &configs) | |
if err != nil { | |
fmt.Printf("JSON Parse Error: %s\n", err) | |
return | |
} | |
fv := func(s string) string { | |
subMat := re.FindStringSubmatch(s) | |
token := subMat[1] | |
configProp := configs[token] | |
switch configProp.(type) { | |
case string: | |
return strconv.Quote(configProp.(string)) | |
case bool: | |
return fmt.Sprintf("%v", configProp) | |
case int, int8, int16, int32, int64, float32, float64: | |
return fmt.Sprintf("%v", configProp) | |
// TODO | |
// case []interface{}: | |
default: | |
return strconv.Quote("") | |
} | |
} | |
template = re.ReplaceAllStringFunc(template, fv) | |
fmt.Println(template) | |
// output | |
// { | |
// "ApplicationLogger": { | |
// "GlobalLogLevel": "INFO", | |
// }, | |
// "InstrumentationNewRelic": { | |
// "Enabled": true, | |
// "License": "xxx-xxx-xxx" | |
// }, | |
// "IntegerAttr": 50, | |
// "MissingAdditionalProp": "" | |
// } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"os" | |
"text/template" | |
) | |
func main() { | |
configJSON := `{ | |
"GlobalLogLevel" : "INFO", | |
"NewRelicEnabled" : true, | |
"NewRelicKey" : "xxx-xxx-xxx", | |
"IntegerAttr" : 50, | |
"AdditionalAttr" : "whatever", | |
"AnArray": ["val1", "val2"] | |
}` | |
configs := make(map[string]interface{}) | |
err := json.Unmarshal([]byte(configJSON), &configs) | |
if err != nil { | |
fmt.Printf("JSON Parse Error: %s\n", err) | |
return | |
} | |
configTemplate := `{ | |
"ApplicationLogger": { | |
"GlobalLogLevel": "{{ .GlobalLogLevel }}", | |
}, | |
"InstrumentationNewRelic": { | |
"Enabled": {{ .NewRelicEnabled }}, | |
"License": "{{ .NewRelicKey }}" | |
}, | |
"IntegerAttr": {{ .IntegerAttr }} | |
}` | |
tmpl, err := template.New("ConfigTemplate").Option("missingkey=error").Parse(configTemplate) | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = tmpl.Execute(os.Stdout, configs) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With Regex:
${AttrName}
in the example)With Text/Template:
{{ .AttrName }}
<no value>
or can be errored out. An arbirtary default value cannot be set.