From cdf9fa5aa2b396b8898e02c30d04cec266961b6b Mon Sep 17 00:00:00 2001 From: risk danger olson Date: Fri, 10 Feb 2017 11:01:54 -0700 Subject: [PATCH] remove lfsapi/schema pkg --- lfsapi/schema/fixture/invalid.json | 5 -- lfsapi/schema/fixture/valid.json | 7 -- lfsapi/schema/reader.go | 103 ----------------------------- lfsapi/schema/reader_test.go | 55 --------------- lfsapi/schema/schema.go | 68 ------------------- lfsapi/schema/schema_test.go | 46 ------------- 6 files changed, 284 deletions(-) delete mode 100644 lfsapi/schema/fixture/invalid.json delete mode 100644 lfsapi/schema/fixture/valid.json delete mode 100644 lfsapi/schema/reader.go delete mode 100644 lfsapi/schema/reader_test.go delete mode 100644 lfsapi/schema/schema.go delete mode 100644 lfsapi/schema/schema_test.go diff --git a/lfsapi/schema/fixture/invalid.json b/lfsapi/schema/fixture/invalid.json deleted file mode 100644 index 09211088..00000000 --- a/lfsapi/schema/fixture/invalid.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-04/schema#", - - "type": "not-a-type" -} diff --git a/lfsapi/schema/fixture/valid.json b/lfsapi/schema/fixture/valid.json deleted file mode 100644 index 2c2915cd..00000000 --- a/lfsapi/schema/fixture/valid.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-04/schema#", - - "type": "number", - "minimum": 0, - "exclusiveMinimum": false -} diff --git a/lfsapi/schema/reader.go b/lfsapi/schema/reader.go deleted file mode 100644 index db2645a3..00000000 --- a/lfsapi/schema/reader.go +++ /dev/null @@ -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) -} diff --git a/lfsapi/schema/reader_test.go b/lfsapi/schema/reader_test.go deleted file mode 100644 index c06c5842..00000000 --- a/lfsapi/schema/reader_test.go +++ /dev/null @@ -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()) -} diff --git a/lfsapi/schema/schema.go b/lfsapi/schema/schema.go deleted file mode 100644 index ba9ec7d3..00000000 --- a/lfsapi/schema/schema.go +++ /dev/null @@ -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 -} diff --git a/lfsapi/schema/schema_test.go b/lfsapi/schema/schema_test.go deleted file mode 100644 index a6ebe46b..00000000 --- a/lfsapi/schema/schema_test.go +++ /dev/null @@ -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) -}