From aa796dc315dececa534dce6a2df7cd303307a0aa Mon Sep 17 00:00:00 2001 From: Luc Talatinian <102624213+lucix-aws@users.noreply.github.com> Date: Fri, 14 Jun 2024 15:59:01 -0400 Subject: [PATCH] drop x/net codegen dependency (#2680) --- .../fc171f917f984c86909e71a6eb1b9b2a.json | 8 ++ internal/codegen/docstring.go | 121 +++++++++--------- internal/codegen/go.mod | 2 - internal/codegen/go.sum | 2 - modman.toml | 1 - 5 files changed, 70 insertions(+), 64 deletions(-) create mode 100644 .changelog/fc171f917f984c86909e71a6eb1b9b2a.json diff --git a/.changelog/fc171f917f984c86909e71a6eb1b9b2a.json b/.changelog/fc171f917f984c86909e71a6eb1b9b2a.json new file mode 100644 index 00000000000..eb5499d6d6b --- /dev/null +++ b/.changelog/fc171f917f984c86909e71a6eb1b9b2a.json @@ -0,0 +1,8 @@ +{ + "id": "fc171f91-7f98-4c86-909e-71a6eb1b9b2a", + "type": "dependency", + "description": "Drop golang.org/x/net codegen dependency.", + "modules": [ + "internal/codegen" + ] +} \ No newline at end of file diff --git a/internal/codegen/docstring.go b/internal/codegen/docstring.go index 8a81794c81c..d12f87d7ce9 100644 --- a/internal/codegen/docstring.go +++ b/internal/codegen/docstring.go @@ -2,14 +2,13 @@ package codegen import ( "bufio" + "encoding/xml" "fmt" "html" "io" + "log" "regexp" "strings" - - xhtml "golang.org/x/net/html" - "golang.org/x/net/html/atom" ) var reNewline = regexp.MustCompile(`\r?\n`) @@ -152,7 +151,11 @@ func getLeadingWhitespace(v string) string { // generateDoc will generate the proper doc string for html encoded or plain text doc entries. func generateDoc(htmlSrc string) string { - tokenizer := xhtml.NewTokenizer(strings.NewReader(htmlSrc)) + tokenizer := xml.NewDecoder(strings.NewReader(htmlSrc)) + tokenizer.Strict = false + tokenizer.AutoClose = xml.HTMLAutoClose + tokenizer.Entity = xml.HTMLEntity + var builder strings.Builder if err := encodeHTMLToText(&builder, tokenizer); err != nil { panic(fmt.Sprintf("failed to generated docs, %v", err)) @@ -168,31 +171,30 @@ type stringWriter interface { WriteString(string) (int, error) } -func encodeHTMLToText(w stringWriter, z *xhtml.Tokenizer) error { +func encodeHTMLToText(w stringWriter, z *xml.Decoder) error { encoder := newHTMLTokenEncoder(w) defer encoder.Flush() for { - tt := z.Next() - if tt == xhtml.ErrorToken { - if err := z.Err(); err == io.EOF { - return nil - } else if err != nil { - return err - } + tt, err := z.Token() + if err == io.EOF { + return nil + } + if err != nil { + return err } - if err := encoder.Encode(z.Token()); err != nil { + if err := encoder.Encode(tt); err != nil { return err } } } type htmlTokenHandler interface { - OnStartTagToken(xhtml.Token) htmlTokenHandler - OnEndTagToken(xhtml.Token, bool) - OnSelfClosingTagToken(xhtml.Token) - OnTextTagToken(xhtml.Token) + OnStartTagToken(xml.StartElement) htmlTokenHandler + OnEndTagToken(xml.Token, bool) + OnSelfClosingTagToken(xml.Token) + OnTextTagToken(xml.CharData) } type htmlTokenEncoder struct { @@ -220,21 +222,21 @@ func newHTMLTokenEncoder(w stringWriter) *htmlTokenEncoder { } func (e *htmlTokenEncoder) Flush() error { - e.baseHandler.handler.OnEndTagToken(xhtml.Token{Type: xhtml.TextToken}, true) + e.baseHandler.handler.OnEndTagToken(xml.CharData([]byte{}), true) return nil } -func (e *htmlTokenEncoder) Encode(token xhtml.Token) error { +func (e *htmlTokenEncoder) Encode(token xml.Token) error { h := e.baseHandler if len(e.handlers) != 0 { h = e.handlers[len(e.handlers)-1] } - switch token.Type { - case xhtml.StartTagToken: + switch v := token.(type) { + case xml.StartElement: e.depth++ - next := h.handler.OnStartTagToken(token) + next := h.handler.OnStartTagToken(v) if next != nil { e.handlers = append(e.handlers, tokenHandlerItem{ handler: next, @@ -242,22 +244,23 @@ func (e *htmlTokenEncoder) Encode(token xhtml.Token) error { }) } - case xhtml.EndTagToken: + case xml.EndElement: handlerBlockClosing := e.depth == h.depth h.handler.OnEndTagToken(token, handlerBlockClosing) // Remove all but the root handler as the handler is no longer needed. - if handlerBlockClosing { + if handlerBlockClosing && len(e.handlers) != 0 { e.handlers = e.handlers[:len(e.handlers)-1] } e.depth-- + if e.depth < 0 { + log.Printf("ignoring unexpected closing tag, %v", token) + e.depth = 0 + } - case xhtml.SelfClosingTagToken: - h.handler.OnSelfClosingTagToken(token) - - case xhtml.TextToken: - h.handler.OnTextTagToken(token) + case xml.CharData: + h.handler.OnTextTagToken(v) } return nil @@ -267,11 +270,11 @@ type baseTokenHandler struct { w stringWriter } -func (e *baseTokenHandler) OnStartTagToken(token xhtml.Token) htmlTokenHandler { return nil } -func (e *baseTokenHandler) OnEndTagToken(token xhtml.Token, blockClosing bool) {} -func (e *baseTokenHandler) OnSelfClosingTagToken(token xhtml.Token) {} -func (e *baseTokenHandler) OnTextTagToken(token xhtml.Token) { - e.w.WriteString(token.Data) +func (e *baseTokenHandler) OnStartTagToken(token xml.StartElement) htmlTokenHandler { return nil } +func (e *baseTokenHandler) OnEndTagToken(token xml.Token, blockClosing bool) {} +func (e *baseTokenHandler) OnSelfClosingTagToken(token xml.Token) {} +func (e *baseTokenHandler) OnTextTagToken(token xml.CharData) { + e.w.WriteString(string(token)) } type blockTokenHandler struct { @@ -295,27 +298,27 @@ func newBlockTokenHandler(w stringWriter) *blockTokenHandler { }, } } -func (e *blockTokenHandler) OnStartTagToken(token xhtml.Token) htmlTokenHandler { +func (e *blockTokenHandler) OnStartTagToken(token xml.StartElement) htmlTokenHandler { e.started = true if e.newlineBeforeNextBlock { e.w.WriteString("\n") e.newlineBeforeNextBlock = false } - switch token.DataAtom { - case atom.A: + switch token.Name.Local { + case "a": return newLinkTokenHandler(e.w, token) - case atom.Ul: + case "ul": e.w.WriteString("\n") e.newlineBeforeNextBlock = true return newListTokenHandler(e.w) - case atom.Div, atom.Dt, atom.P, atom.H1, atom.H2, atom.H3, atom.H4, atom.H5, atom.H6: + case "div", "dt", "p", "h1", "h2", "h3", "h4", "h5", "h6": e.w.WriteString("\n") e.newlineBeforeNextBlock = true return newBlockTokenHandler(e.w) - case atom.Pre, atom.Code: + case "pre", "code": if e.rootBlock { e.w.WriteString("\n") e.w.WriteString(indent) @@ -326,7 +329,7 @@ func (e *blockTokenHandler) OnStartTagToken(token xhtml.Token) htmlTokenHandler return nil } -func (e *blockTokenHandler) OnEndTagToken(token xhtml.Token, blockClosing bool) { +func (e *blockTokenHandler) OnEndTagToken(token xml.Token, blockClosing bool) { if !blockClosing { return } @@ -340,15 +343,15 @@ func (e *blockTokenHandler) OnEndTagToken(token xhtml.Token, blockClosing bool) e.strBuilder.Reset() } -func (e *blockTokenHandler) OnTextTagToken(token xhtml.Token) { +func (e *blockTokenHandler) OnTextTagToken(token xml.CharData) { if e.newlineBeforeNextBlock { e.w.WriteString("\n") e.newlineBeforeNextBlock = false } if !e.started { - token.Data = strings.TrimLeft(token.Data, " \t\n") + token = xml.CharData(strings.TrimLeft(string(token), " \t\n")) } - if len(token.Data) != 0 { + if len(token) != 0 { e.started = true } e.baseTokenHandler.OnTextTagToken(token) @@ -356,10 +359,10 @@ func (e *blockTokenHandler) OnTextTagToken(token xhtml.Token) { type linkTokenHandler struct { baseTokenHandler - linkToken xhtml.Token + linkToken xml.StartElement } -func newLinkTokenHandler(w stringWriter, token xhtml.Token) *linkTokenHandler { +func newLinkTokenHandler(w stringWriter, token xml.StartElement) *linkTokenHandler { return &linkTokenHandler{ baseTokenHandler: baseTokenHandler{ w: w, @@ -367,7 +370,7 @@ func newLinkTokenHandler(w stringWriter, token xhtml.Token) *linkTokenHandler { linkToken: token, } } -func (e *linkTokenHandler) OnEndTagToken(token xhtml.Token, blockClosing bool) { +func (e *linkTokenHandler) OnEndTagToken(token xml.Token, blockClosing bool) { if !blockClosing { return } @@ -390,9 +393,9 @@ func newListTokenHandler(w stringWriter) *listTokenHandler { }, } } -func (e *listTokenHandler) OnStartTagToken(token xhtml.Token) htmlTokenHandler { - switch token.DataAtom { - case atom.Li: +func (e *listTokenHandler) OnStartTagToken(token xml.StartElement) htmlTokenHandler { + switch token.Name.Local { + case "li": if e.items >= 1 { e.w.WriteString("\n\n") } @@ -402,7 +405,7 @@ func (e *listTokenHandler) OnStartTagToken(token xhtml.Token) htmlTokenHandler { return nil } -func (e *listTokenHandler) OnTextTagToken(token xhtml.Token) { +func (e *listTokenHandler) OnTextTagToken(token xml.CharData) { // Squash whitespace between list and items } @@ -423,14 +426,14 @@ func newListItemTokenHandler(w stringWriter) *listItemTokenHandler { }, } } -func (e *listItemTokenHandler) OnStartTagToken(token xhtml.Token) htmlTokenHandler { - switch token.DataAtom { - case atom.P: +func (e *listItemTokenHandler) OnStartTagToken(token xml.StartElement) htmlTokenHandler { + switch token.Name.Local { + case "p": return newBlockTokenHandler(e.w) } return nil } -func (e *listItemTokenHandler) OnEndTagToken(token xhtml.Token, blockClosing bool) { +func (e *listItemTokenHandler) OnEndTagToken(token xml.Token, blockClosing bool) { if !blockClosing { return } @@ -456,7 +459,7 @@ func newTrimSpaceTokenHandler(w stringWriter) *trimSpaceTokenHandler { }, } } -func (e *trimSpaceTokenHandler) OnEndTagToken(token xhtml.Token, blockClosing bool) { +func (e *trimSpaceTokenHandler) OnEndTagToken(token xml.Token, blockClosing bool) { if !blockClosing { return } @@ -464,10 +467,10 @@ func (e *trimSpaceTokenHandler) OnEndTagToken(token xhtml.Token, blockClosing bo e.origWriter.WriteString(strings.TrimSpace(e.strBuilder.String())) } -func getHTMLTokenAttr(attr []xhtml.Attribute, name string) (string, bool) { +func getHTMLTokenAttr(attr []xml.Attr, name string) (string, bool) { for _, a := range attr { - if strings.EqualFold(a.Key, name) { - return a.Val, true + if strings.EqualFold(a.Name.Local, name) { + return a.Value, true } } return "", false diff --git a/internal/codegen/go.mod b/internal/codegen/go.mod index 70ecf26fbfe..3cfc46059ce 100644 --- a/internal/codegen/go.mod +++ b/internal/codegen/go.mod @@ -3,5 +3,3 @@ module github.com/aws/aws-sdk-go-v2/internal/codegen/defaults go 1.20 replace github.com/aws/aws-sdk-go-v2 => ../../ - -require golang.org/x/net v0.23.0 diff --git a/internal/codegen/go.sum b/internal/codegen/go.sum index 199e74ae801..e69de29bb2d 100644 --- a/internal/codegen/go.sum +++ b/internal/codegen/go.sum @@ -1,2 +0,0 @@ -golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= -golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= diff --git a/modman.toml b/modman.toml index 16efa2e9cfe..1cc06eeeba8 100644 --- a/modman.toml +++ b/modman.toml @@ -3,7 +3,6 @@ "github.com/aws/aws-sdk-go" = "v1.44.28" "github.com/aws/smithy-go" = "v1.20.2" "github.com/jmespath/go-jmespath" = "v0.4.0" - "golang.org/x/net" = "v0.23.0" [modules]