remove lfsapi/schema pkg

This commit is contained in:
risk danger olson 2017-02-10 11:01:54 -07:00
parent 5a55c028a8
commit cdf9fa5aa2
6 changed files with 0 additions and 284 deletions

@ -1,5 +0,0 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "not-a-type"
}

@ -1,7 +0,0 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "number",
"minimum": 0,
"exclusiveMinimum": false
}

@ -1,103 +0,0 @@
package schema
import (
"bytes"
"io"
"strings"
"sync/atomic"
"github.com/git-lfs/git-lfs/errors"
"github.com/xeipuuv/gojsonschema"
)
var (
// errValidationIncomplete is an error returned when `ValidationErr()`
// is called while the reader is still processing data.
errValidationIncomplete = errors.New("lfsapi/schema: validation incomplete")
)
// state represents the set of valid states a `*Reader` (see below) can be in
type state uint32
const (
// stateNotStarted means the `*Reader` has no processed any data
stateNotStarted state = iota
// stateProcessing means the `*Reader` has received a `Read()` call at
// least once, but has not received an `io.EOF` yet.
stateProcessing
// stateProcessed means the `*Reader` has received a `Read()` call at
// least once and has gotten an `io.EOF`, meaning there is no more data
// to process.
stateProcessed
)
type Reader struct {
// r is the underlying io.Reader this one is wrapping.
r io.Reader
// buf is the buffer of data read from the underlying reader
buf *bytes.Buffer
// schema is the *gojsonschema.Schema to valid the buffer against
schema *gojsonschema.Schema
// state is the current state that this `*Reader` is in, and is updated
// atomically through `atomic.SetUint32`, and etc.
state uint32
// result stores the result of the schema validation
result *gojsonschema.Result
// resultErr stores the (optional) error returned from the schema
// validation
resultErr error
}
var _ io.Reader = (*Reader)(nil)
// Read implements io.Reader.Read, and returns exactly the data received from
// the underlying reader.
//
// Read also sometimes advances state, as according to the valid instances of
// the `state` from above. If transitioning into the `stateProcessed` state, the
// schema will be validated.
func (r *Reader) Read(p []byte) (n int, err error) {
atomic.CompareAndSwapUint32(&r.state, uint32(stateNotStarted), uint32(stateProcessing))
n, err = r.r.Read(p)
if err == io.EOF {
got := gojsonschema.NewStringLoader(r.buf.String())
r.result, r.resultErr = r.schema.Validate(got)
atomic.CompareAndSwapUint32(&r.state, uint32(stateProcessing), uint32(stateProcessed))
}
return
}
// ValidationErr returns an error assosciated with validating the data. If
// there was an error performing the validation itself, that error will be
// returned with priority. If the validation has not started, or is incomplete,
// an appropriate error will be returned.
//
// Otherwise, if any validation errors were present, an error will be returned
// containing all of the validation errors. If the data passed validation, a
// value of 'nil' will be returned instead.
func (r *Reader) ValidationErr() error {
if r.resultErr != nil {
return r.resultErr
} else {
switch state(atomic.LoadUint32(&r.state)) {
case stateNotStarted, stateProcessing:
return errValidationIncomplete
}
}
if r.result.Valid() {
return nil
}
msg := "Validation errors:\n"
for _, e := range r.result.Errors() {
msg = strings.Join([]string{msg, e.Description()}, "\n")
}
return errors.New(msg)
}

@ -1,55 +0,0 @@
package schema
import (
"io"
"io/ioutil"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSchemaReaderWithValidPayload(t *testing.T) {
schema, err := FromJSON(ValidSchemaPath)
require.Nil(t, err)
r := schema.Reader(strings.NewReader("1"))
io.Copy(ioutil.Discard, r)
assert.Nil(t, r.ValidationErr())
}
func TestSchemaReaderWithInvalidPayload(t *testing.T) {
schema, err := FromJSON(ValidSchemaPath)
require.Nil(t, err)
r := schema.Reader(strings.NewReader("-1"))
io.Copy(ioutil.Discard, r)
assert.NotNil(t, r.ValidationErr())
}
func TestSchemaReaderBeforeValidation(t *testing.T) {
schema, err := FromJSON(ValidSchemaPath)
require.Nil(t, err)
r := schema.Reader(strings.NewReader("1"))
assert.Equal(t, errValidationIncomplete, r.ValidationErr())
}
func TestSchemaReaderDuringValidation(t *testing.T) {
schema, err := FromJSON(ValidSchemaPath)
require.Nil(t, err)
r := schema.Reader(strings.NewReader("12"))
var b [1]byte
n, err := r.Read(b[:])
assert.Equal(t, 1, n)
assert.Nil(t, err)
assert.Equal(t, errValidationIncomplete, r.ValidationErr())
}

@ -1,68 +0,0 @@
package schema
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"github.com/xeipuuv/gojsonschema"
)
// Schema holds a JSON schema to be used for validation against various
// payloads.
type Schema struct {
// s is the internal handle on the implementation of the JSON schema
// specification.
s *gojsonschema.Schema
}
// FromJSON constructs a new `*Schema` instance from the JSON schema at
// `schemaPath` relative to the package this code was called from.
//
// If the file could not be accessed, or was unable to be parsed as a valid JSON
// schema, an appropriate error will be returned. Otherwise, the `*Schema` will
// be returned with a nil error.
func FromJSON(schemaPath string) (*Schema, error) {
dir, err := os.Getwd()
if err != nil {
return nil, err
}
dir = filepath.ToSlash(dir)
schemaPath = filepath.Join(dir, schemaPath)
if _, err := os.Stat(schemaPath); err != nil {
return nil, err
}
schema, err := gojsonschema.NewSchema(gojsonschema.NewReferenceLoader(
// Platform compatibility: use "/" separators always for file://
fmt.Sprintf("file:///%s", filepath.ToSlash(schemaPath)),
))
if err != nil {
return nil, err
}
return &Schema{schema}, nil
}
// Reader wraps the given `io.Reader`, "r" as a `*schema.Reader`, allowing the
// contents passed through the reader to be inspected as conforming to the JSON
// schema or not.
//
// If the reader "r" already _is_ a `*schema.Reader`, it will be returned as-is.
func (s *Schema) Reader(r io.Reader) *Reader {
if sr, ok := r.(*Reader); ok {
return sr
}
rdr := &Reader{
buf: new(bytes.Buffer),
schema: s.s,
}
rdr.r = io.TeeReader(r, rdr.buf)
return rdr
}

@ -1,46 +0,0 @@
package schema
import (
"bytes"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
ValidSchemaPath = "fixture/valid.json"
InvalidSchemaPath = "fixture/invalid.json"
MissingSchemaPath = "fixture/missing.json"
)
func TestCreatingAValidSchema(t *testing.T) {
_, err := FromJSON(ValidSchemaPath)
assert.Nil(t, err)
}
func TestCreatingAMissingSchema(t *testing.T) {
_, err := FromJSON(MissingSchemaPath)
assert.NotNil(t, err)
assert.True(t, os.IsNotExist(err))
}
func TestCreatingAnInvalidSchema(t *testing.T) {
_, err := FromJSON(InvalidSchemaPath)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "not-a-type is not a valid type")
}
func TestWrappingASchemaReader(t *testing.T) {
s, err := FromJSON(ValidSchemaPath)
require.Nil(t, err)
sr := s.Reader(new(bytes.Buffer))
wrapped := s.Reader(sr)
assert.Equal(t, sr, wrapped)
}