dmitri.shuralyov.com/service/change/...

fs: Filter by change state in List.

Also return no error and empty result in List, Count when repo spec
doesn't match.
dmitshur committed 6 years ago commit 8de1a4e157b79a3eeb3faa5e1576ed1d63af6db7
Collapse all
fs/fs.go
@@ -69,23 +69,38 @@ var s = struct {
}

// List changes.
func (*Service) List(ctx context.Context, repo string, opt change.ListOptions) ([]change.Change, error) {
	if repo != "dmitri.shuralyov.com/font/woff2" {
		return nil, os.ErrNotExist
		return nil, nil
	}
	var counts func(s change.State) bool
	switch opt.Filter {
	case change.FilterOpen:
		counts = func(s change.State) bool { return s == change.OpenState }
	case change.FilterClosedMerged:
		counts = func(s change.State) bool { return s == change.ClosedState || s == change.MergedState }
	case change.FilterAll:
		counts = func(s change.State) bool { return true }
	default:
		// TODO: Map to 400 Bad Request HTTP error.
		return nil, fmt.Errorf("opt.State has unsupported value %q", opt.Filter)
	}
	var cs []change.Change
	for _, c := range s.changes {
		if !counts(c.State) {
			continue
		}
		cs = append(cs, c.Change)
	}
	return cs, nil
}

// Count changes.
func (*Service) Count(ctx context.Context, repo string, opt change.ListOptions) (uint64, error) {
	if repo != "dmitri.shuralyov.com/font/woff2" {
		return 0, os.ErrNotExist
		return 0, nil
	}
	var counts func(s change.State) bool
	switch opt.Filter {
	case change.FilterOpen:
		counts = func(s change.State) bool { return s == change.OpenState }
@@ -97,13 +112,14 @@ func (*Service) Count(ctx context.Context, repo string, opt change.ListOptions)
		// TODO: Map to 400 Bad Request HTTP error.
		return 0, fmt.Errorf("opt.State has unsupported value %q", opt.Filter)
	}
	var count uint64
	for _, c := range s.changes {
		if counts(c.State) {
			count++
		if !counts(c.State) {
			continue
		}
		count++
	}
	return count, nil
}

// Get a change.