From 64933127b3f3c2c7f0f36c19e9c9484e53b2e18b Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 12 Apr 2020 17:15:22 -0400 Subject: [PATCH] api: add blob objects --- graphql/graph/generated/generated.go | 10 ++-- graphql/graph/model/blob.go | 75 ++++++++++++++++++++++++++++ graphql/graph/model/models_gen.go | 27 ---------- graphql/graph/model/object.go | 9 ++++ graphql/graph/schema.graphqls | 2 + 5 files changed, 92 insertions(+), 31 deletions(-) create mode 100644 graphql/graph/model/blob.go diff --git a/graphql/graph/generated/generated.go b/graphql/graph/generated/generated.go index 6656c8d..7b796a0 100644 --- a/graphql/graph/generated/generated.go +++ b/graphql/graph/generated/generated.go @@ -1286,10 +1286,12 @@ enum BlobType { } type BinaryBlob { + # TODO: Consdier adding a range specifier base64: String! } type TextBlob { + # TODO: Consdier adding a range specifier text: String! } @@ -2467,13 +2469,13 @@ func (ec *executionContext) _Blob_blobType(ctx context.Context, field graphql.Co Object: "Blob", Field: field, Args: nil, - IsMethod: false, + IsMethod: true, } ctx = graphql.WithFieldContext(ctx, fc) resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.BlobType, nil + return obj.BlobType(), nil }) if err != nil { ec.Error(ctx, err) @@ -2501,13 +2503,13 @@ func (ec *executionContext) _Blob_data(ctx context.Context, field graphql.Collec Object: "Blob", Field: field, Args: nil, - IsMethod: false, + IsMethod: true, } ctx = graphql.WithFieldContext(ctx, fc) resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.Data, nil + return obj.Data(), nil }) if err != nil { ec.Error(ctx, err) diff --git a/graphql/graph/model/blob.go b/graphql/graph/model/blob.go new file mode 100644 index 0000000..d7d7389 --- /dev/null +++ b/graphql/graph/model/blob.go @@ -0,0 +1,75 @@ +package model + +import ( + "errors" + "io/ioutil" + "unicode/utf8" + + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing/object" +) + +type Blob struct { + Type ObjectType `json:"type"` + ID string `json:"id"` + ShortID string `json:"shortId"` + Raw string `json:"raw"` + + blob *object.Blob + repo *git.Repository + bytes []byte +} + +func (Blob) IsObject() {} + +type BlobData interface { + IsBlobData() +} + +type BinaryBlob struct { + Base64 string `json:"base64"` +} + +func (BinaryBlob) IsBlobData() {} + +type TextBlob struct { + Text string `json:"text"` +} + +func (TextBlob) IsBlobData() {} + +func (blob *Blob) Bytes() []byte { + if blob.bytes != nil { + return blob.bytes + } + reader, err := blob.blob.Reader() + if err != nil { + panic(err) + } + defer reader.Close() + // XXX: Probably a bad idea + blob.bytes, err = ioutil.ReadAll(reader) + if err != nil { + panic(err) + } + return blob.bytes +} + +func (blob *Blob) BlobType() BlobType { + if utf8.ValidString(string(blob.Bytes())) { + return BlobTypeText + } else { + return BlobTypeBinary + } +} + +func (blob *Blob) Data() BlobData { + switch blob.BlobType() { + case BlobTypeBinary: + panic(errors.New("Unimplemented")) + case BlobTypeText: + return &TextBlob{Text: string(blob.Bytes())} + default: + panic(errors.New("Unknown blob type")) + } +} diff --git a/graphql/graph/model/models_gen.go b/graphql/graph/model/models_gen.go index 80b77a1..cc9fcb8 100644 --- a/graphql/graph/model/models_gen.go +++ b/graphql/graph/model/models_gen.go @@ -9,10 +9,6 @@ import ( "time" ) -type BlobData interface { - IsBlobData() -} - type Entity interface { IsEntity() } @@ -35,23 +31,6 @@ type Artifact struct { URL string `json:"url"` } -type BinaryBlob struct { - Base64 string `json:"base64"` -} - -func (BinaryBlob) IsBlobData() {} - -type Blob struct { - Type ObjectType `json:"type"` - ID string `json:"id"` - ShortID string `json:"shortId"` - Raw string `json:"raw"` - BlobType BlobType `json:"blobType"` - Data BlobData `json:"data"` -} - -func (Blob) IsObject() {} - type FilterBy struct { Terms string `json:"terms"` } @@ -81,12 +60,6 @@ type Tag struct { func (Tag) IsObject() {} -type TextBlob struct { - Text string `json:"text"` -} - -func (TextBlob) IsBlobData() {} - type Version struct { Major int `json:"major"` Minor int `json:"minor"` diff --git a/graphql/graph/model/object.go b/graphql/graph/model/object.go index 92d90ff..8d976ae 100644 --- a/graphql/graph/model/object.go +++ b/graphql/graph/model/object.go @@ -38,6 +38,15 @@ func LookupObject(repo *git.Repository, hash plumbing.Hash) (Object, error) { tree: obj, repo: repo, }, nil + case *object.Blob: + return &Blob{ + Type: ObjectTypeBlob, + ID: obj.ID().String(), + ShortID: obj.ID().String()[:7], + + blob: obj, + repo: repo, + }, nil default: return nil, errors.New("Unknown object type") } diff --git a/graphql/graph/schema.graphqls b/graphql/graph/schema.graphqls index b0b0be4..975f3fc 100644 --- a/graphql/graph/schema.graphqls +++ b/graphql/graph/schema.graphqls @@ -196,10 +196,12 @@ enum BlobType { } type BinaryBlob { + # TODO: Consdier adding a range specifier base64: String! } type TextBlob { + # TODO: Consdier adding a range specifier text: String! } -- 2.38.4