-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use bytes.Buffer instead channels and goroutines #7
Open
gaydin
wants to merge
1
commit into
udacity:master
Choose a base branch
from
gaydin:buffer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,23 @@ | ||
package graphb | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
) | ||
|
||
type argumentValue interface { | ||
stringChan() <-chan string | ||
stringChan(buffer *bytes.Buffer) | ||
} | ||
|
||
type Argument struct { | ||
Name string | ||
Value argumentValue | ||
} | ||
|
||
func (a *Argument) stringChan() <-chan string { | ||
tokenChan := make(chan string) | ||
go func() { | ||
tokenChan <- a.Name | ||
tokenChan <- ":" | ||
for str := range a.Value.stringChan() { | ||
tokenChan <- str | ||
} | ||
close(tokenChan) | ||
}() | ||
return tokenChan | ||
func (a *Argument) stringChan(buffer *bytes.Buffer) { | ||
buffer.WriteString(a.Name) | ||
buffer.WriteString(":") | ||
a.Value.stringChan(buffer) | ||
} | ||
|
||
func ArgumentAny(name string, value interface{}) (Argument, error) { | ||
|
@@ -85,143 +79,99 @@ func ArgumentCustomTypeSliceElem(values ...Argument) []Argument { | |
return values | ||
} | ||
|
||
///////////////////////////// | ||
// /////////////////////////// | ||
// Primitive Wrapper Types // | ||
///////////////////////////// | ||
// /////////////////////////// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. haha, go fmt |
||
|
||
// argBool represents a boolean value. | ||
type argBool bool | ||
|
||
func (v argBool) stringChan() <-chan string { | ||
tokenChan := make(chan string) | ||
go func() { | ||
tokenChan <- fmt.Sprintf("%t", v) | ||
close(tokenChan) | ||
}() | ||
return tokenChan | ||
func (v argBool) stringChan(buffer *bytes.Buffer) { | ||
buffer.WriteString(fmt.Sprintf("%t", v)) | ||
} | ||
|
||
// argInt represents an integer value. | ||
type argInt int | ||
|
||
func (v argInt) stringChan() <-chan string { | ||
tokenChan := make(chan string) | ||
go func() { | ||
tokenChan <- fmt.Sprintf("%d", v) | ||
close(tokenChan) | ||
}() | ||
return tokenChan | ||
func (v argInt) stringChan(buffer *bytes.Buffer) { | ||
buffer.WriteString(fmt.Sprintf("%d", v)) | ||
} | ||
|
||
// argString represents a string value. | ||
type argString string | ||
|
||
func (v argString) stringChan() <-chan string { | ||
tokenChan := make(chan string) | ||
go func() { | ||
tokenChan <- fmt.Sprintf(`"%s"`, v) | ||
close(tokenChan) | ||
}() | ||
return tokenChan | ||
func (v argString) stringChan(buffer *bytes.Buffer) { | ||
buffer.WriteString(fmt.Sprintf(`"%s"`, v)) | ||
} | ||
|
||
////////////////////////////////// | ||
// //////////////////////////////// | ||
// Primitive List Wrapper Types // | ||
////////////////////////////////// | ||
// //////////////////////////////// | ||
|
||
// argBoolSlice implements valueSlice | ||
type argBoolSlice []bool | ||
|
||
func (s argBoolSlice) stringChan() <-chan string { | ||
tokenChan := make(chan string) | ||
go func() { | ||
tokenChan <- "[" | ||
for i, v := range s { | ||
if i != 0 { | ||
tokenChan <- "," | ||
} | ||
tokenChan <- fmt.Sprintf("%t", v) | ||
func (s argBoolSlice) stringChan(buffer *bytes.Buffer) { | ||
buffer.WriteString("[") | ||
for i, v := range s { | ||
if i != 0 { | ||
buffer.WriteString(",") | ||
} | ||
tokenChan <- "]" | ||
close(tokenChan) | ||
}() | ||
return tokenChan | ||
buffer.WriteString(fmt.Sprintf("%t", v)) | ||
} | ||
buffer.WriteString("]") | ||
} | ||
|
||
// argIntSlice implements valueSlice | ||
type argIntSlice []int | ||
|
||
func (s argIntSlice) stringChan() <-chan string { | ||
tokenChan := make(chan string) | ||
go func() { | ||
tokenChan <- "[" | ||
for i, v := range s { | ||
if i != 0 { | ||
tokenChan <- "," | ||
} | ||
tokenChan <- fmt.Sprintf("%d", v) | ||
func (s argIntSlice) stringChan(buffer *bytes.Buffer) { | ||
buffer.WriteString("[") | ||
for i, v := range s { | ||
if i != 0 { | ||
buffer.WriteString(",") | ||
} | ||
tokenChan <- "]" | ||
close(tokenChan) | ||
}() | ||
return tokenChan | ||
buffer.WriteString(fmt.Sprintf("%d", v)) | ||
} | ||
buffer.WriteString("]") | ||
} | ||
|
||
// argStringSlice implements valueSlice | ||
type argStringSlice []string | ||
|
||
func (s argStringSlice) stringChan() <-chan string { | ||
tokenChan := make(chan string) | ||
go func() { | ||
tokenChan <- "[" | ||
for i, v := range s { | ||
if i != 0 { | ||
tokenChan <- "," | ||
} | ||
tokenChan <- fmt.Sprintf(`"%s"`, v) | ||
func (s argStringSlice) stringChan(buffer *bytes.Buffer) { | ||
buffer.WriteString("[") | ||
for i, v := range s { | ||
if i != 0 { | ||
buffer.WriteString(",") | ||
} | ||
tokenChan <- "]" | ||
close(tokenChan) | ||
}() | ||
return tokenChan | ||
buffer.WriteString(fmt.Sprintf(`"%s"`, v)) | ||
} | ||
buffer.WriteString("]") | ||
} | ||
|
||
type argumentSlice []Argument | ||
|
||
func (s argumentSlice) stringChan() <-chan string { | ||
tokenChan := make(chan string) | ||
go func() { | ||
tokenChan <- "{" | ||
for i, v := range s { | ||
if i != 0 { | ||
tokenChan <- "," | ||
} | ||
for str := range v.stringChan() { | ||
tokenChan <- str | ||
} | ||
func (s argumentSlice) stringChan(buffer *bytes.Buffer) { | ||
buffer.WriteString("{") | ||
for i, v := range s { | ||
if i != 0 { | ||
buffer.WriteString(",") | ||
} | ||
tokenChan <- "}" | ||
close(tokenChan) | ||
}() | ||
return tokenChan | ||
v.stringChan(buffer) | ||
} | ||
buffer.WriteString("}") | ||
} | ||
|
||
type argCustomTypeSlice [][]Argument | ||
|
||
func (s argCustomTypeSlice) stringChan() <-chan string { | ||
tokenChan := make(chan string) | ||
go func() { | ||
tokenChan <- "[" | ||
for i, v := range s { | ||
if i != 0 { | ||
tokenChan <- "," | ||
} | ||
for str := range argumentSlice(v).stringChan() { | ||
tokenChan <- str | ||
} | ||
func (s argCustomTypeSlice) stringChan(buffer *bytes.Buffer) { | ||
buffer.WriteString("[") | ||
for i, v := range s { | ||
if i != 0 { | ||
buffer.WriteString(",") | ||
} | ||
tokenChan <- "]" | ||
close(tokenChan) | ||
}() | ||
return tokenChan | ||
argumentSlice(v).stringChan(buffer) | ||
} | ||
buffer.WriteString("]") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fantastic. I wanted to do this when I first created it. But I was too happy about the clever use of channels & goroutine so that I left it there. But I do think an
io.Writer
alike version is better.Some suggestions:
stringWriter() *bytes.Buffer
which returns an writer instead of consuming one?io.Writer
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function name could be
stringWriter
so that people don't get confused.