Skip to content

Commit

Permalink
✨ feat: stdio - add some commonly io interface definition
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 5, 2023
1 parent b439a24 commit 50b5445
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
7 changes: 7 additions & 0 deletions comdef/types.go
Expand Up @@ -52,6 +52,13 @@ type Compared interface {
Int | Uint | Float | ~string
}

// SimpleType interface type. alias of ScalarType
//
// contains: (x)int, float, ~string, ~bool types
type SimpleType interface {
Int | Uint | Float | ~string | ~bool
}

// ScalarType interface type.
//
// it can be ordered, that supports the operators < <= >= >.
Expand Down
21 changes: 21 additions & 0 deletions jsonutil/jsonbuild.go
@@ -0,0 +1,21 @@
package jsonutil

/*
TODO json build
type JsonBuilder struct {
Indent string
// mu sync.Mutex
// cfg *CConfig
buf bytes.Buffer
out io.Writer
}
// AddField add field to json
func (b *JsonBuilder) AddField(key string, value any) *JsonBuilder {
b.buf.WriteString(`,"`)
b.buf.WriteString(key)
b.buf.WriteString(`":`)
b.encode(value)
return b
}
*/
30 changes: 30 additions & 0 deletions stdio/iface.go
@@ -0,0 +1,30 @@
package stdio

import "io"

// Flusher interface
type Flusher interface {
Flush() error
}

// FlushWriter is the interface satisfied by logging destinations.
type FlushWriter interface {
Flusher
// Writer the output writer
io.Writer
}

// FlushCloseWriter is the interface satisfied by logging destinations.
type FlushCloseWriter interface {
Flusher
// WriteCloser the output writer
io.WriteCloser
}

// SyncCloseWriter is the interface satisfied by logging destinations.
// such as os.File
type SyncCloseWriter interface {
Flusher
// WriteCloser the output writer
io.WriteCloser
}
2 changes: 1 addition & 1 deletion stdio/stdio.go
Expand Up @@ -82,7 +82,7 @@ func WriteString(s string) {
_, _ = os.Stdout.WriteString(s)
}

// Writeln to stdout
// Writeln string to stdout
func Writeln(s string) {
_, _ = os.Stdout.WriteString(s)
_, _ = os.Stdout.Write([]byte("\n"))
Expand Down
4 changes: 4 additions & 0 deletions stdio/stdio_test.go
Expand Up @@ -32,6 +32,10 @@ func TestNewIOReader(t *testing.T) {
assert.Eq(t, "hi", stdio.ReadString(r))
r = stdio.NewIOReader(strings.NewReader("hi"))
assert.Eq(t, "hi", stdio.ReadString(r))

r = stdio.NewIOReader("hi")
stdio.DiscardReader(r)
assert.Eq(t, "", stdio.ReadString(r))
}

func TestWriteBytes(t *testing.T) {
Expand Down

0 comments on commit 50b5445

Please sign in to comment.