### Install go-jsval package Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/jsval/README.md Command to install the `go-jsval` Go package using `go get`. ```shell go get -u github.com/lestrrat-go/jsval ``` -------------------------------- ### Install stripe-mock binary Source: https://github.com/stripe/stripe-mock/blob/master/README.md Installs the stripe-mock executable using Go's `go install` command, fetching the latest version from GitHub. ```sh go install github.com/stripe/stripe-mock@latest ``` -------------------------------- ### Install jsval command-line tool Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/jsval/README.md Command to install the `jsval` command-line executable, which is part of the `go-jsval` project, using `go get`. ```shell go get -u github.com/lestrrat-go/jsval/cmd/jsval ``` -------------------------------- ### Install and Manage Stripe-Mock with Homebrew Source: https://github.com/stripe/stripe-mock/blob/master/README.md Instructions for installing, starting, upgrading, and restarting the stripe-mock service using Homebrew. The Homebrew service listens on port 12111 for HTTP and 12112 for HTTPS/HTTP/2. ```sh brew install stripe/stripe-mock/stripe-mock # start a stripe-mock service at login brew services start stripe-mock # upgrade if you already have it brew upgrade stripe-mock # restart the service after upgrading brew services restart stripe-mock ``` -------------------------------- ### Run jsval playground server Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/jsval/README.md Command to start the `jsval` playground server, which allows users to specify a JSON schema and visualize the generated validator. It listens on the specified address and port. ```shell jsval server -listen :8080 ``` -------------------------------- ### Make a Sample Request to Stripe-Mock Source: https://github.com/stripe/stripe-mock/blob/master/README.md Example curl command to send a sample request to the running stripe-mock service on localhost:12111, including an Authorization header for testing. ```sh curl -i http://localhost:12111/v1/charges -H "Authorization: Bearer sk_test_123" ``` -------------------------------- ### Run stripe-mock with automatic port selection Source: https://github.com/stripe/stripe-mock/blob/master/README.md Starts the stripe-mock server, instructing it to automatically select an available HTTP port by passing `0`. ```sh stripe-mock -http-port 0 ``` -------------------------------- ### Run stripe-mock with Unix sockets Source: https://github.com/stripe/stripe-mock/blob/master/README.md Starts the stripe-mock server, configuring it to listen for HTTP and HTTPS requests over specified Unix domain sockets instead of TCP ports. ```sh stripe-mock -http-unix /tmp/stripe-mock.sock -https-unix /tmp/stripe-mock-secure.sock ``` -------------------------------- ### Get value using JSON pointer in Go Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/jspointer/README.md Demonstrates how to create a new JSON pointer instance and use its Get method to retrieve a value from a Go struct, map, or slice based on the pointer path. ```Go p, _ := jspointer.New(`/foo/bar/baz`) result, _ := p.Get(someStruct) ``` -------------------------------- ### Run stripe-mock with explicit ports Source: https://github.com/stripe/stripe-mock/blob/master/README.md Starts the stripe-mock server, allowing explicit specification of HTTP and HTTPS listening ports. Either port can be omitted to listen on only one protocol. ```sh stripe-mock -http-port 12111 -https-port 12112 ``` -------------------------------- ### Run stripe-mock with default ports Source: https://github.com/stripe/stripe-mock/blob/master/README.md Starts the stripe-mock server, listening for HTTP requests on port 12111 and HTTPS requests on port 12112 by default. ```sh stripe-mock ``` -------------------------------- ### Parse and Validate JSON Schema in Go Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/jsschema/README.md This Go example demonstrates how to read a JSON Schema file using `schema.ReadFile`, iterate through its properties, and then validate an arbitrary data structure against the loaded schema using `validator.New` and `v.Validate`. ```go package schema_test import ( "log" "github.com/lestrrat-go/jsschema" "github.com/lestrrat-go/jsschema/validator" ) func Example() { s, err := schema.ReadFile("schema.json") if err != nil { log.Printf("failed to read schema: %s", err) return } for name, pdef := range s.Properties { // Do what you will with `pdef`, which contain // Schema information for `name` property _ = name _ = pdef } // You can also validate an arbitrary piece of data var p interface{} // initialize using json.Unmarshal... v := validator.New(s) if err := v.Validate(p); err != nil { log.Printf("failed to validate data: %s", err) } } ``` -------------------------------- ### Example JSON Schema with internal JSON References Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/jsval/README.md An example JSON Schema demonstrating the use of internal JSON references (`$ref`) to reuse schema definitions. Here, the 'age' property references a 'positiveInteger' definition within the same document. ```json { "definitions": { "positiveInteger": { "type": "integer", "minimum": 0 } }, "properties": { "age": { "$ref": "#/definitions/positiveInteger" } } } ``` -------------------------------- ### Programmatically build Go validator from JSON Schema file Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/jsval/README.md Illustrates how to read a JSON schema file using `jsschema` and then build a `jsval` validator programmatically in Go. The example shows the process of reading the schema, building the validator, and then using it to validate an input. ```go package jsval_test import ( "log" "github.com/lestrrat-go/jsschema" "github.com/lestrrat-go/jsval/builder" ) func ExampleBuild() { s, err := schema.ReadFile(`/path/to/schema.json`) if err != nil { log.Printf("failed to open schema: %s", err) return } b := builder.New() v, err := b.Build(s) if err != nil { log.Printf("failed to build validator: %s", err) return } var input interface{} if err := v.Validate(input); err != nil { log.Printf("validation failed: %s", err) return } } ``` -------------------------------- ### Adding Context to Go Errors with errors.Wrap Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/pkg/errors/README.md This example demonstrates how to use the `errors.Wrap` function from the `pkg/errors` package. It returns a new error that wraps the original error, adding a descriptive context string to the failure path without altering the original error's value. ```Go _, err := ioutil.ReadAll(r) if err != nil { return errors.Wrap(err, "read failed") } ``` -------------------------------- ### Manually construct a Go validator with jsval fluent API Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/jsval/README.md Shows how to build a `jsval` validator manually using its fluent API. This example defines an object validator with specific properties, their types (string, regexp string), and required fields, then demonstrates its usage for validation. ```go func ExampleManual() { v := jsval.Object(). AddProp(`zip`, jsval.String().RegexpString(`^\d{5}$`)). AddProp(`address`, jsval.String()). AddProp(`name`, jsval.String()). AddProp(`phone_number`, jsval.String().RegexpString(`^[\d-]+$`)). Required(`zip`, `address`, `name`) var input interface{} if err := v.Validate(input); err != nil { log.Printf("validation failed: %s", err) return } } ``` -------------------------------- ### Go: Log Errors Conditionally with pdebug.Marker.BindError Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/pdebug/README.md This Go example shows how to integrate error logging with `pdebug.Marker` using `BindError`. It automatically logs the returned error value only if an error occurs, providing concise debugging information for functions that return errors. ```Go func Foo() (err error) { if pdebug.Enabled { g := pdebug.Marker("Foo").BindError(&err) defer g.End() } pdebug.Printf("Inside Foo()!\n") return errors.New("boo") } ``` -------------------------------- ### Retrieving Original Error Cause with errors.Cause Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/pkg/errors/README.md This example shows how to use `errors.Cause` to retrieve the original, topmost error from a chain of wrapped errors. It recursively unwraps errors until it finds one that does not implement the `causer` interface, allowing for specific error type handling. ```Go switch err := errors.Cause(err).(type) { case *MyError: // handle specifically default: // unknown error } ``` -------------------------------- ### Run Benchmarks for JSON Schema Parsing Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/jsschema/README.md This snippet shows how to execute performance benchmarks for the `jsschema` package using `go test`. It includes flags for benchmark execution, memory allocation tracking, and comparison against `gojsonschema`. ```shell $ go test -tags benchmark -benchmem -bench=. BenchmarkGojsonschemaParse-4 5000 357330 ns/op 167451 B/op 1866 allocs/op BenchmarkParse-4 1000000 1577 ns/op 1952 B/op 9 allocs/op BenchmarkParseAndMakeValidator-4 500000 2806 ns/op 2304 B/op 13 allocs/op PASS ``` -------------------------------- ### Bundle New Certificate into Go Executable Source: https://github.com/stripe/stripe-mock/blob/master/embedded/cert/README.md After generating a new certificate, this command uses `go generate` to re-bundle the certificate into a `*.go` file. This is necessary because certificates are bundled directly with the executable using `go-bindata`. ```bash go generate ``` -------------------------------- ### Run Go Test Suite for Stripe-Mock Source: https://github.com/stripe/stripe-mock/blob/master/README.md Command to execute the Go test suite for the stripe-mock project, ensuring all tests pass. This is typically run during development. ```sh go test ./... ``` -------------------------------- ### Go: Structured Logging with pdebug.Marker Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/pdebug/README.md This Go snippet illustrates the use of `pdebug.Marker` to create structured, indented debug logs for function calls. The `defer g.End()` ensures proper termination and timing of the marked block, making call chains visually clearer. ```Go func Foo() { if pdebug.Enabled { g := pdebug.Marker("Foo") defer g.End() } pdebug.Printf("Inside Foo()!\n") } ``` -------------------------------- ### Shell: Enable Debug Trace for Go Tests Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/pdebug/README.md This shell command shows how to activate the debug trace output from `go-pdebug` during Go test execution. It requires setting the `PDEBUG_TRACE=1` environment variable and compiling with the `-tags debug` build tag. ```Shell # For example, to show debug code during testing: PDEBUG_TRACE=1 go test -tags debug ``` -------------------------------- ### Traditional Go Error Handling Idiom Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/pkg/errors/README.md This snippet illustrates the common, traditional Go idiom for handling errors. While simple, this approach often results in error reports that lack sufficient context or debugging information when errors propagate up the call stack. ```Go if err != nil { return err } ``` -------------------------------- ### Tag and Push Release for Stripe-Mock Source: https://github.com/stripe/stripe-mock/blob/master/README.md Commands to pull existing tags, create a new version tag (e.g., v0.1.1), and push tags to origin. This process triggers automatic releases via Travis CI and goreleaser. ```sh git pull origin --tags git tag v0.1.1 git push origin --tags ``` -------------------------------- ### Run Stripe-Mock using Docker Source: https://github.com/stripe/stripe-mock/blob/master/README.md Command to run stripe-mock in a Docker container, mapping ports 12111-12112. The default Docker ENTRYPOINT listens on these ports for HTTP and HTTPS/HTTP/2. ```sh docker run --rm -it -p 12111-12112:12111-12112 stripe/stripe-mock:latest ``` -------------------------------- ### Resolve JSON References in Go Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/jsref/README.md Demonstrates how to use the `jsref` library to resolve JSON references within a document, including internal and external references. It shows how to register an in-memory provider and use `WithRecursiveResolution` for full resolution of all resulting values. ```go package jsref_test import ( "encoding/json" "fmt" "log" jsref "github.com/lestrrat-go/jsref" "github.com/lestrrat-go/jsref/provider" ) func Example() { var v interface{} src := []byte(` { "foo": ["bar", {"$ref": "#/sub"}, {"$ref": "obj2#/sub"}], "sub": "baz" }`) if err := json.Unmarshal(src, &v); err != nil { log.Printf("%s", err) return } // External reference mp := provider.NewMap() mp.Set("obj2", map[string]string{"sub": "quux"}) res := jsref.New() res.AddProvider(mp) // Register the provider data := []struct { Ptr string Options []jsref.Option }{ { Ptr: "#/foo/0", // "bar" }, { Ptr: "#/foo/1", // "baz" }, { Ptr: "#/foo/2", // "quux" (resolves via `mp`) }, { Ptr: "#/foo", // ["bar",{"$ref":"#/sub"},{"$ref":"obj2#/sub"}] }, { Ptr: "#/foo", // ["bar","baz","quux"] // experimental option to resolve all resulting values Options: []jsref.Option{ jsref.WithRecursiveResolution(true) }, }, } for _, set := range data { result, err := res.Resolve(v, set.Ptr, set.Options...) if err != nil { // failed to resolve fmt.Printf("err: %s\n", err) continue } b, _ := json.Marshal(result) fmt.Printf("%s -> %s\n", set.Ptr, string(b)) } // OUTPUT: // #/foo/0 -> "bar" // #/foo/1 -> "baz" // #/foo/2 -> "quux" // #/foo -> ["bar",{"$ref":"#/sub"},{"$ref":"obj2#/sub"}] // #/foo -> ["bar","baz","quux"] } ``` -------------------------------- ### Shell: Force Debug Trace Output in Go Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/pdebug/README.md This command demonstrates how to forcefully enable `go-pdebug` trace output without relying on environment variables. Using the `-tags debug0` build tag is convenient for direct debugging and testing scenarios. ```Shell go test -tags debug0 ``` -------------------------------- ### Available JSON Reference Providers Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/jsref/README.md Details the different types of providers that can be registered with the `jsref.Resolver` to handle external JSON references. These providers define how the resolver fetches external documents. ```APIDOC Provider: Name: provider.FS Description: Resolve from local file system. References must start with a `file:///` prefix Provider: Name: provider.Map Description: Resolve from in memory map. Provider: Name: provider.HTTP Description: Resolve by making HTTP requests. References must start with a `http(s?)://` prefix ``` -------------------------------- ### Generate Go validator from JSON Schema using jsval CLI Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/jsval/README.md Demonstrates how to use the `jsval` command-line tool to generate a Go validator file (`validator.go`) from a JSON schema file. It also shows how to specify multiple JSON pointers to extract specific schemas from a larger document like a Hyper Schema. ```shell jsval -s /path/to/schema.json -o validator.go jsval -s /path/to/hyperschema.json -o validator.go -p "/links/0/schema" -p "/links/1/schema" -p "/links/2/schema" ``` -------------------------------- ### Generate Go validator file from JSON Schema Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/jsval/README.md Demonstrates the `jsval` command-line tool's ability to generate a Go file (`jsval.go`) containing `*jsval.JSVal` structures directly from a JSON schema definition, making them available for inclusion in Go code. ```shell jsval -s schema.json -o jsval.go ``` -------------------------------- ### Update OpenAPI Specification for Stripe-Mock Source: https://github.com/stripe/stripe-mock/blob/master/README.md Command to update the OpenAPI specification for stripe-mock. This task is performed by running a make command in the root of the repository. ```sh make update-openapi-spec ``` -------------------------------- ### Defining the causer Interface for Error Cause Retrieval Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/pkg/errors/README.md This snippet defines the `causer` interface, which is used by the `pkg/errors` package. Any error value that implements this interface can be inspected by `errors.Cause` to retrieve its underlying original error, forming a chain of contextual errors. ```Go type causer interface { Cause() error } ``` -------------------------------- ### Go: Conditionally Compile Debug Code with pdebug.Enabled Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/pdebug/README.md This Go snippet demonstrates how to use the `pdebug.Enabled` constant to include or exclude debug code based on build tags. Code within the `if pdebug.Enabled` block is only compiled when the `-tags debug` flag is used during compilation. ```Go func Foo() { // will only be available if you compile with `-tags debug` if pdebug.Enabled { pdebug.Printf("Starting Foo()!\n") } } ``` -------------------------------- ### Generate Go validators from specific JSON pointers in a document Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/jsval/README.md Shows how to use the `-p` argument with the `jsval` command to extract and generate validators from specific JSON schema definitions embedded within a larger JSON document, such as a JSON Hyper Schema, ensuring proper JSON reference resolution. ```shell jsval -s hyper.json -p "/links/0" -p "/lnks/1" ``` -------------------------------- ### Generate Self-Signed Certificate for Localhost Source: https://github.com/stripe/stripe-mock/blob/master/embedded/cert/README.md This command generates a new self-signed X.509 certificate and an RSA 4096-bit private key. The certificate is valid for 3650 days (10 years) and is issued for 'localhost'. The key is saved to 'cert/key.pem' and the certificate to 'cert/cert.pem'. ```bash openssl req -x509 -newkey rsa:4096 -keyout cert/key.pem -out cert/cert.pem -days 3650 -nodes -subj '/CN=localhost' ``` -------------------------------- ### Define optional struct fields for jsval validation using Maybe interface Source: https://github.com/stripe/stripe-mock/blob/master/vendor/github.com/lestrrat-go/jsval/README.md Illustrates how to define struct fields as optional for `jsval` validation using the `Maybe` interface (e.g., `MaybeString`). This allows the validation mechanism to correctly handle fields that may or may not be initialized in a Go struct, similar to how map properties are checked. ```go type Foo struct { Name MaybeString `json:"name"` } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.