qvalue

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2023 License: MIT Imports: 5 Imported by: 0

README

QValues

Document

QValues is a parser for quality values. It's commonly used in HTTP Headers like Accept, Accept-Language, Accept-Encoding etc.

The quality value this package provided is multiplied by 1000, to avoid floating point sorting.

The parser can also return sorted quality values(bigger quality first), if two item has same quality, the one that occurs first takes precedence.

I write this package just for my own convenience, but if you have any suggestion, issues and PRs are both welcome.

Install

go get github.com/7sDream/qvalue

Example

package main

import (
    "github.com/7sDream/qvalue"
)

var qvParser = qvalue.NewOptions()

func main() {
    acceptEncoding := "gzip, deflate;q=0.8, br;q=0.7, identity;q=0.5, *;q=0.1"
    // If you do not care quality, just want deal them in origin order, use `Parse` instead
    qvs, _ := qvParser.ParseAndSort(acceptEncoding)
    for _, qv := range qvs {
        // For example, we can only deal with gzip and identity
        switch qv.Value {
        case "gzip":
            {
                /* ... */
                break
            }
        case "*", "identity":
            {
                /* ... */
                break
            }
        }
    }
}

License

MIT.

Documentation

Overview

Package qvalue is a parser for [quality values](https://developer.mozilla.org/en-US/docs/Glossary/Quality_values).

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Error to indicate there is invalid quality in provided string
	QualityParseError = errors.New("Quality is not a valid number")
	// Error io indicate a quality is out of range
	QualityRangeError = errors.New("Quality should be in range of [0.0, 1.0]")
)

Functions

func Sort added in v0.1.1

func Sort(qvs []*QValue)

Sort a quality value list by items' quality, item which occurs first takes precedence if quality is same.

Types

type Options

type Options struct {
	// contains filtered or unexported fields
}

Options is configure for this parser.

func NewOptions

func NewOptions() *Options

Create a new default parser option. The default behavior is report any error and default quality is 1.0.

func (*Options) IgnoreQualityParseError

func (o *Options) IgnoreQualityParseError(value uint) *Options

IgnoreQualityParseError makes this parse option ignore `QualityParseError`, and set the item's quality to `value` if this error happened.

func (*Options) IgnoreQualityRangeError

func (o *Options) IgnoreQualityRangeError(value uint) *Options

IgnoreQualityParseError makes this parse option ignore `QualityRangeError` and set the item's quality to `value` if this error happened.

func (*Options) Parse

func (o *Options) Parse(s string) ([]*QValue, error)

Parse a quality value series.

Example
package main

import (
	"fmt"

	"github.com/7sDream/qvalue"
)

func main() {
	o := qvalue.NewOptions()
	qvs, _ := o.Parse("gzip, deflate;q=0.8, br;q=0.7, identity;q=0.5, *;q=0.1")
	for _, qv := range qvs {
		fmt.Printf("%s: %d\n", qv.Value, qv.Quality)
	}
}
Output:

gzip: 1000
deflate: 800
br: 700
identity: 500
*: 100

func (*Options) ParseAndSort

func (o *Options) ParseAndSort(s string) ([]*QValue, error)

Parse a quality value series, and sort items according to their quality.

func (*Options) SetDefaultQuality

func (o *Options) SetDefaultQuality(value uint) *Options

SetDefaultQuality set the default quality if a item do not have it. The default is 1.0, according to the spec.

type QValue

type QValue struct {
	Value   string
	Quality uint
	Index   int
}

QValue is a quality value.

The `Index` field is it's origin index.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL