~edwargix/git.sr.ht

64933127b3f3c2c7f0f36c19e9c9484e53b2e18b — Drew DeVault 5 years ago afa86ab
api: add blob objects
M graphql/graph/generated/generated.go => graphql/graph/generated/generated.go +6 -4
@@ 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)

A graphql/graph/model/blob.go => graphql/graph/model/blob.go +75 -0
@@ 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"))
	}
}

M graphql/graph/model/models_gen.go => graphql/graph/model/models_gen.go +0 -27
@@ 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"`

M graphql/graph/model/object.go => graphql/graph/model/object.go +9 -0
@@ 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")
	}

M graphql/graph/schema.graphqls => graphql/graph/schema.graphqls +2 -0
@@ 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!
}