..

How to use python in ultisnips

This snippet uses python to convert camel case to underline. It is an example to use python in ultisnips. t[1] gets content of $1. After the convert, assign the result to snip.rv.

snippet gfk "gorm foreign key"
$1ID uint \`gorm:"column:`!p name=re.sub(r"(\w)([A-Z])", r"\1_\2", t[1]).lower(); snip.rv = name`_id" json:"-"\`
${1:Model} $1 \`gorm:"foreignKey:$1ID" json:"`!p name=re.sub(r"(\w)([A-Z])", r"\1_\2", t[1]).lower(); snip.rv = name`"\`
endsnippet