Formatting complex AST code generation

180 views
Skip to first unread message

Amir Omidi (aaomidi)

unread,
Nov 10, 2023, 11:09:02 PM11/10/23
to golang-nuts
Hi all,

Background is that I've been trying to tackle the issue here: https://github.com/zmap/zlint/issues/765

Effectively, there is a code signature change where I want do a large scale code change using AST to make old code conform to a new signature.

I have the majority of this working, and it's been successfully modifying the files. However, I've been struggling with getting the formatting looking right.


The output goes from:

```
func init() {
lint.RegisterLint(&lint.Lint{
Name: "e_rsa_fermat_factorization",
Description: "RSA key pairs that are too close to each other are susceptible to the Fermat Factorization " +
"Method (for more information please see https://en.wikipedia.org/wiki/Fermat%27s_factorization_method " +
"and https://fermatattack.secvuln.info/)",
Citation:      "Pierre de Fermat",
Source:        lint.Community,
EffectiveDate: util.ZeroDate,
Lint:          NewFermatFactorization,
})
}

```
to:

```
func init() {
lint.RegisterCertificateLint(&lint.CertificateLint{LintMetadata: lint.LintMetadata{Name: "e_rsa_fermat_factorization",
Description: "RSA key pairs that are too close to each other are susceptible to the Fermat Factorization " +
"Method (for more information please see https://en.wikipedia.org/wiki/Fermat%27s_factorization_method " +
"and https://fermatattack.secvuln.info/)",
Citation: "Pierre de Fermat",
Source: lint.Community,
EffectiveDate: util.ZeroDate}, Lint: NewFermatFactorization})

}

```
That output is not how people are used to seeing structs, how should I get it to be formatted correctly?

Amir Omidi (aaomidi)

unread,
Nov 12, 2023, 1:15:50 PM11/12/23
to golang-nuts
Hi all,

I solved this problem by switching from go/ast to https://github.com/dave/dst. I still think something like this belongs in the stdlib.e
Reply all
Reply to author
Forward
0 new messages