Skip to content

Commit

Permalink
Duplicate internal dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Apr 25, 2024
1 parent 59d748b commit 754bdc3
Show file tree
Hide file tree
Showing 36 changed files with 251 additions and 32 deletions.
9 changes: 9 additions & 0 deletions x/sqlite/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"recommendations": [
"emeraldwalk.runonsave",
"tamasfe.even-better-toml",
"github.vscode-github-actions",
"codezombiech.gitignore",
"golang.go"
]
}
11 changes: 11 additions & 0 deletions x/sqlite/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"go.lintTool": "golangci-lint",
"emeraldwalk.runonsave": {
"commands": [
{
"match": "\\.go$",
"cmd": "golangci-lint run -c ${workspaceFolder}/.golangci-safe.toml ${fileDirname} --fix"
}
]
}
}
2 changes: 1 addition & 1 deletion x/sqlite/changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

"github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal/mock"
"github.com/go-kivik/kivik/x/sqlite/v4/internal/mock"
)

func TestDBChanges(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"testing"

"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal/mock"
"github.com/go-kivik/kivik/x/sqlite/v4/internal/mock"
)

type DB interface {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"net/http"

"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal"
"github.com/go-kivik/kivik/x/sqlite/v4/internal"
)

func (d *db) Delete(ctx context.Context, docID string, options driver.Options) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal/mock"
"github.com/go-kivik/kivik/x/sqlite/v4/internal/mock"
)

func TestDBDelete(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/deleteattachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"net/http"

"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal"
"github.com/go-kivik/kivik/x/sqlite/v4/internal"
)

func (d *db) DeleteAttachment(ctx context.Context, docID, filename string, options driver.Options) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/deleteattachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

"github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal/mock"
"github.com/go-kivik/kivik/x/sqlite/v4/internal/mock"
)

func TestDBDeleteAttachment(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/designdocs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

"github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal/mock"
"github.com/go-kivik/kivik/x/sqlite/v4/internal/mock"
)

func TestDBDesignDocs(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

"github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal/mock"
"github.com/go-kivik/kivik/x/sqlite/v4/internal/mock"
)

func TestDBGet(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/getattachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"net/http"

"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal"
"github.com/go-kivik/kivik/x/sqlite/v4/internal"
)

func (d *db) GetAttachment(ctx context.Context, docID string, filename string, options driver.Options) (*driver.Attachment, error) {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/getattachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

"github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal/mock"
"github.com/go-kivik/kivik/x/sqlite/v4/internal/mock"
)

func TestDBGetAttachment(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/getattachmentmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"net/http"

"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal"
"github.com/go-kivik/kivik/x/sqlite/v4/internal"
)

func (d *db) GetAttachmentMeta(ctx context.Context, docID, filename string, options driver.Options) (*driver.Attachment, error) {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/getattachmentmeta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal/mock"
"github.com/go-kivik/kivik/x/sqlite/v4/internal/mock"
)

func TestDBGetAttachmentMeta(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/getrev.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"net/http"

"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal"
"github.com/go-kivik/kivik/x/sqlite/v4/internal"
)

func (d *db) GetRev(ctx context.Context, id string, options driver.Options) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/getrev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal/mock"
"github.com/go-kivik/kivik/x/sqlite/v4/internal/mock"
)

func TestGetRev(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/go-kivik/kivik/v4/x/sqlite
module github.com/go-kivik/kivik/x/sqlite/v4

go 1.22.0

Expand Down
173 changes: 173 additions & 0 deletions x/sqlite/internal/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// Package internal provides some internal utilities.
package internal

import (
"errors"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
)

// CompositeError represents an HTTP status, encoded in the first byte as the
// status - 400, plus the error message.
type CompositeError string

func (c CompositeError) Error() string {
return "kivik: " + string(c[4:])
}

// HTTPStatus returns c's HTTP status code.
func (c CompositeError) HTTPStatus() int {
i, _ := strconv.Atoi(string(c[:3]))
return i
}

// Error represents an error returned by Kivik.
//
// This type definition is not guaranteed to remain stable, or even exported.
// When examining errors programatically, you should rely instead on the
// HTTPStatus() function in this package, rather than on directly observing
// the fields of this type.
type Error struct {
// Status is the HTTP status code associated with this error. Normally
// this is the actual HTTP status returned by the server, but in some cases
// it may be generated by Kivik directly.
Status int

// Message is the error message.
Message string

// Err is the originating error, if any.
Err error
}

var (
_ error = &Error{}
_ statusCoder = &Error{}
)

func (e *Error) Error() string {
if e.Err == nil {
return e.msg()
}
if e.Message == "" {
return e.Err.Error()
}
return e.Message + ": " + e.Err.Error()
}

// HTTPStatus returns the HTTP status code associated with the error, or 500
// (internal server error), if none.
func (e *Error) HTTPStatus() int {
if e.Status == 0 {
return http.StatusInternalServerError
}
return e.Status
}

// Unwrap satisfies the errors wrapper interface.
func (e *Error) Unwrap() error {
return e.Err
}

// Format implements [fmt.Formatter].
func (e *Error) Format(f fmt.State, c rune) {
const partsLen = 3
parts := make([]string, 0, partsLen)
if e.Message != "" {
parts = append(parts, e.Message)
}
if c == 'v' {
if f.Flag('+') {
parts = append(parts, fmt.Sprintf("%d / %s", e.Status, http.StatusText(e.Status)))
}
}
if e.Err != nil {
parts = append(parts, e.Err.Error())
}
_, _ = fmt.Fprint(f, strings.Join(parts, ": "))
}

func (e *Error) msg() string {
switch e.Message {
case "":
return http.StatusText(e.HTTPStatus())
default:
return e.Message
}
}

type statusCoder interface {
HTTPStatus() int
}

// HTTPStatus returns the HTTP status code embedded in the error, or 500
// (internal server error), if there was no specified status code. If err is
// nil, HTTPStatus returns 0.
func HTTPStatus(err error) int {
if err == nil {
return 0
}
var coder statusCoder
for {
if errors.As(err, &coder) {
return coder.HTTPStatus()
}
if uw := errors.Unwrap(err); uw != nil {
err = uw
continue
}
return http.StatusInternalServerError
}
}

// StatusErrorDiff returns the empty string if the expected error string and
// status match err. Otherwise, it returns a description of the mismatch.
func StatusErrorDiff(wantErr string, wantStatus int, err error) string {
var (
msg string
status int
)
if err != nil {
status = HTTPStatus(err)
msg = err.Error()
}
if msg != wantErr || status != wantStatus {
return fmt.Sprintf("Unexpected error: %s [%d] (expected: %s [%d])",
err, status, wantErr, wantStatus)
}
return ""
}

// StatusErrorDiffRE returns the empty string if the expected error RE and
// status match err. Otherwise, it returns a description of the mismatch.
func StatusErrorDiffRE(wantErrRE string, wantStatus int, err error) string {
re := regexp.MustCompile(wantErrRE)
var (
msg string
status int
)
if err != nil {
status = HTTPStatus(err)
msg = err.Error()
}
if !re.MatchString(msg) || status != wantStatus {
return fmt.Sprintf("Unexpected error: %s [%d] (expected: %s [%d])",
err, status, re, wantStatus)
}
return ""
}
26 changes: 26 additions & 0 deletions x/sqlite/internal/mock/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// Package mock is a partial duplicate of [github.com/go-kivik/v4/internal/mock].
package mock

import "github.com/go-kivik/kivik/v4/driver"

type nilOption bool

var _ driver.Options = nilOption(false)

func (nilOption) Apply(interface{}) {}
func (nilOption) String() string { return "NilOption" }

// NilOption is a nil option.
const NilOption nilOption = false
2 changes: 1 addition & 1 deletion x/sqlite/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"strconv"
"strings"

"github.com/go-kivik/kivik/v4/internal"
"github.com/go-kivik/kivik/x/sqlite/v4/internal"
)

type revision struct {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/localdocs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

"github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal/mock"
"github.com/go-kivik/kivik/x/sqlite/v4/internal/mock"
)

func TestDBLocalDocs(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/openrevs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strings"

"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal"
"github.com/go-kivik/kivik/x/sqlite/v4/internal"
)

func (d *db) OpenRevs(ctx context.Context, docID string, revs []string, options driver.Options) (driver.Rows, error) {
Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/openrevs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/driver"
"github.com/go-kivik/kivik/v4/internal/mock"
"github.com/go-kivik/kivik/x/sqlite/v4/internal/mock"
)

func TestDBOpenRevs(t *testing.T) {
Expand Down
Loading

0 comments on commit 754bdc3

Please sign in to comment.