yagpdb
yagpdb copied to clipboard
Suggestion: standardize build in function
While i use this piece of code in CC (edited by Satty today because it stopped working because some database functionality change on interface types) that works great BTW.
{{/*Converter for interfase types from DB*/}}
{{- define "standardize"}}{{/*Thanks to Satty*/}}
{{- $value:= (.Get "val")}}{{$recursiveSdict := sdict}}{{$rangeVal := ""}}
{{- if (eq (printf "%T" $value) `map[interface {}]interface {}`)}}{{$rangeVal = sdict $value}}
{{- else if (eq (printf "%T" $value) `templates.SDict`)}}{{$rangeVal = $value}}
{{- else if (eq (printf "%T" $value) `[]interface {}`)}}{{$rangeVal = cslice.AppendSlice $value}}
{{- else if (eq (printf "%T" $value) `templates.Slice`)}}{{$rangeVal = $value}}{{end}}
{{- if (print $rangeVal)}}
{{- range $k,$v := $rangeVal}}
{{- if in (cslice `map[interface {}]interface {}` `[]interface {}` `templates.SDict` `templates.Slice`) (printf "%T" $value) }}
{{- $recursiveSdict.Set "val" $v}}{{template "standardize" $recursiveSdict}}{{$rangeVal.Set $k ($recursiveSdict.Get "ret")}}{{end}}{{end}}
{{- else}}{{$rangeVal = $value}}{{end}}
{{- (.Set "ret" $rangeVal)}}
{{- end}}
I did use the old version to help out others in the help chat on discord, meaning I got some DM from people because the CC didn't work anymore. Like any code that is written, it should be backward compatible, and with the awesome ability to use a DB and stuff I feel the need for a build-in converter when changes are made to the way DB save or return data. Meaning when changes occur they can be adjusted at the same time and cause no need for changes to be made to an existing script. (unless it needed security-wise or any other vulnerability of course)
That why I suggest a way to have a built-in function that handles this probably better in terms of bot performance and provides easy use in code. I see 2 possibilities:
- DBEntry variant to .Value called something like .StandarizedValue
Example
{{$x:= (dbGet userID key).StandarizedValue}}
- A function Standarize
Example:
{{$x:= (Standarize (dbGet userID key).Value)}}
Any Thoughts?