Skip to content

Commit

Permalink
drop x/net codegen dependency (#2680)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucix-aws committed Jun 14, 2024
1 parent bc5bd7c commit aa796dc
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 64 deletions.
8 changes: 8 additions & 0 deletions .changelog/fc171f917f984c86909e71a6eb1b9b2a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "fc171f91-7f98-4c86-909e-71a6eb1b9b2a",
"type": "dependency",
"description": "Drop golang.org/x/net codegen dependency.",
"modules": [
"internal/codegen"
]
}
121 changes: 62 additions & 59 deletions internal/codegen/docstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -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))
Expand All @@ -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 {
Expand Down Expand Up @@ -220,44 +222,45 @@ 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,
depth: e.depth,
})
}

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
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -340,34 +343,34 @@ 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)
}

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,
},
linkToken: token,
}
}
func (e *linkTokenHandler) OnEndTagToken(token xhtml.Token, blockClosing bool) {
func (e *linkTokenHandler) OnEndTagToken(token xml.Token, blockClosing bool) {
if !blockClosing {
return
}
Expand All @@ -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")
}
Expand All @@ -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
}

Expand All @@ -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
}
Expand All @@ -456,18 +459,18 @@ 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
}

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
Expand Down
2 changes: 0 additions & 2 deletions internal/codegen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions internal/codegen/go.sum
Original file line number Diff line number Diff line change
@@ -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=
1 change: 0 additions & 1 deletion modman.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down

0 comments on commit aa796dc

Please sign in to comment.