score-spec / score-go

Reference library for parsing and loading Score files
Apache License 2.0
26 stars 6 forks source link
go score

ci Go Reference

score-go

Reference library containing common types and functions for building Score implementations in Go.

This can be added to your project via:

go get -u github.com/score-spec/score-go@latest

NOTE: if you project is still using the hand-written types, you will need to stay on github.com/score-spec/score-go@v0.0.1 and any important fixes to the schema may be back-ported to that branch.

Packages

Parsing SCORE files

This library includes a few utility methods to parse source SCORE files.

import (
    "os"

    "gopkg.in/yaml.v3"

    scoreloader "github.com/score-spec/score-go/loader"
    scoreschema "github.com/score-spec/score-go/schema"
    scoretypes "github.com/score-spec/score-go/types"
)

func main() {
    src, err := os.Open("score.yaml")
    if err != nil {
        panic(err)
    }
    defer src.Close()

    var srcMap map[string]interface{}
    if err := yaml.NewDecoder(src).Decode(&srcMap); err != nil {
        panic(err)
    }

    if err := scoreschema.Validate(srcMap); err != nil {
        panic(err)
    }

    var spec scoretypes.Workload
    if err := scoreloader.MapSpec(&spec, srcMap); err != nil {
        panic(err)
    }

    if err := scoreloader.Normalize(&spec, "."); err != nil {
        panic(err)
    }

    // Do something with the spec
    // ...
}

Building a Score implementation

score-compose is the reference Score implementation written in Go and using this library. If you'd like to write a custom Score implementation, use the functions in this library and the score-compose implementation as a Guide.

Upgrading the schema version

When the Score JSON schema is updated in https://github.com/score-spec/spec, this repo should be updated to match.

First copy the new score-v1b1.json and samples/ files from the spec repo, into schema/files.

Then regenerate the defined types:

make generate

And ensure the tests still pass:

go test -v ./...