github.com/go-graphite/carbonapi@v0.17.0/scripts/json_to_go_struct.sh (about) 1 #!/bin/bash 2 3 # Disclaimer: 4 # This code is veeeeery ugly, but it works. If it hurt your feelings, please rewrite it in some normal language. 5 # I'm truly sorry that I decided to write that in bash and I really hope that next time I'll make better descision 6 # 7 # Main idea of this file is to get graphite-web's json and autogenerate description as a Go-struct. 8 9 usage() { 10 echo "${0} func_list_output.json function_dir code_path" 11 echo 12 echo "Main idea of this file is to get graphite-web's /functions handler json output and autogenerate description as a Go-struct." 13 echo "Example: ./${0} ./func.json below ~/go/gopath_third_party/src/github.com/go-graphite/carbonapi" 14 } 15 16 JSON_FILE=${1} 17 if [[ -z ${JSON_FILE} ]]; then 18 usage 19 exit 1 20 fi 21 shift 22 FUNCTION_DIR="${1}" 23 if [[ -z ${FUNCTION_DIR} ]]; then 24 usage 25 exit 1 26 fi 27 28 shift 29 # ~/go/gopath_third_party/src/github.com/go-graphite/carbonapi 30 CODE_PATH="${1}" 31 if [[ -z ${CODE_PATH} ]]; then 32 usage 33 exit 1 34 fi 35 36 shift 37 38 REWRITE="${1}" 39 FUNCTIONS_DIR_PATH="expr/functions" 40 if [[ ${REWRITE} == 1 ]]; then 41 FUNCTIONS_DIR_PATH="expr/rewrite" 42 fi 43 44 FUNCTIONS=$(egrep 'RegisterFunction|functions :=' "${CODE_PATH}"/"${FUNCTIONS_DIR_PATH}"/"${FUNCTION_DIR}"/function.go | grep -v 'RegisterFunction(f,' | egrep -o '"[^"]+"' | tr -d '"') 45 46 { 47 echo 48 echo "// Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web" 49 echo "func (f *${FUNCTION_DIR}) Description() map[string]types.FunctionDescription {" 50 echo "return map[string]types.FunctionDescription{" 51 for NAME in ${FUNCTIONS}; do 52 JSON=$(jq ".[\"${NAME}\"]" "${JSON_FILE}") 53 echo "\"${NAME}\": {" 54 awk '{l[NR] = $0} END {for (i=2; i<=NR-1; i++) print l[i]}' <<< "${JSON}" | sed -r 's/^(\s*)"([^"]+)":(.*)/\1\2:\3/g;s/^\s*\<./\U&/g;s/\]/}/g;s/("|})\s*$/\1,/g;s/Params: \[/Params: []types.FunctionParam{/;s#Type: "([^"]+)"#Type: types.\u\1#g;s#(Options|Suggestions): \[#\1: []string{#g;s# Default: ([^"]+),$# Default: "\1",#g;s#(\s*)([.0-9]+),\s*$#\1"\2",#g;s#(true|false)$#\1,#g' 55 echo 56 echo "}," 57 done 58 echo "}" 59 echo "}" 60 } # | gofmt