Skip to content

Commit

Permalink
Remove fs related stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Jan 29, 2021
1 parent d5a02fc commit bc69aee
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 423 deletions.
130 changes: 30 additions & 100 deletions internal/cmd/genreadfile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
)

type definition struct {
Filename string // go >= 1.16
FallbackFilename string // go < 1.16
Package string
ReturnType string
ParseOptions bool
Filename string
Package string
ReturnType string
ParseOptions bool
}

func main() {
Expand All @@ -27,122 +26,52 @@ func main() {
func _main() error {
definitions := []definition{
{
Package: "jwk",
ReturnType: "Set",
Filename: "jwk/io.go",
FallbackFilename: "jwk/io_go1.15.go",
Package: "jwk",
ReturnType: "Set",
Filename: "jwk/io.go",
},
{
Package: "jws",
ReturnType: "*Message",
Filename: "jws/io.go",
FallbackFilename: "jws/io_go1.15.go",
Package: "jws",
ReturnType: "*Message",
Filename: "jws/io.go",
},
{
Package: "jwe",
ReturnType: "*Message",
Filename: "jwe/io.go",
FallbackFilename: "jwe/io_go1.15.go",
Package: "jwe",
ReturnType: "*Message",
Filename: "jwe/io.go",
},
{
Package: "jwt",
ReturnType: "Token",
Filename: "jwt/io.go",
FallbackFilename: "jwt/io_go1.15.go",
ParseOptions: true,
Package: "jwt",
ReturnType: "Token",
Filename: "jwt/io.go",
ParseOptions: true,
},
}

for _, def := range definitions {
if err := generateFile(def); err != nil {
return err
}
if err := generateFallbackFile(def); err != nil {
return err
}
}
return nil
}

func generateFile(def definition) error {
var buf bytes.Buffer
fmt.Fprintf(&buf, "// +build go1.16")
fmt.Fprintf(&buf, "\n\n// Automatically generated by internal/cmd/genreadfile/main.go. DO NOT EDIT")
fmt.Fprintf(&buf, "\n\npackage %s", def.Package)
fmt.Fprintf(&buf, "\n\nimport \"github.com/lestrrat-go/jwx/internal/fs\"")
fmt.Fprintf(&buf, "\n\n// ReadFileOption describes an option that can be passed to `ReadFile`")
fmt.Fprintf(&buf, "\ntype ReadFileOption = fs.OpenOption")
fmt.Fprintf(&buf, "\n\n// LocalFS is used to explicitly tell ReadFile to read from the")
fmt.Fprintf(&buf, "\n// local file system")
fmt.Fprintf(&buf, "\ntype LocalFS = fs.Local")
fmt.Fprintf(&buf, "\n\n// WithFS creates an option that specifies where to load files from")
fmt.Fprintf(&buf, "\n// when `ReadFile()` is called. For example, you can specify an")
fmt.Fprintf(&buf, "\n// instance of `embed.FS` to load files from an embedded file")
fmt.Fprintf(&buf, "\n// system in a compiled binary.")
fmt.Fprintf(&buf, "\nfunc WithFS(v fs.FS) ReadFileOption {")
fmt.Fprintf(&buf, "\nreturn fs.WithFS(v)")
fmt.Fprintf(&buf, "\n}")
fmt.Fprintf(&buf, "\n\n// ReadFile reads a JWK set at the specified location.")
fmt.Fprintf(&buf, "\n//")
fmt.Fprintf(&buf, "\n// For go >= 1.16 where io/fs is supported, you may pass `WithFS()` option")
fmt.Fprintf(&buf, "\n// to provide an alternate location to load the files from. This means")
fmt.Fprintf(&buf, "\n// you can embed your JWK in your compiled program and read it back from")
fmt.Fprintf(&buf, "\n// the embedded resource.")
fmt.Fprintf(&buf, "\n//")
fmt.Fprintf(&buf, "\n// By default files are read from the local file system by using `os.Open`")
fmt.Fprintf(&buf, "\nfunc ReadFile(path string, options ...ReadFileOption) (%s, error) {", def.ReturnType)
if def.ParseOptions {
fmt.Fprintf(&buf, "\nvar parseOptions []ParseOption")
fmt.Fprintf(&buf, "\nvar readFileOptions []ReadFileOption")
fmt.Fprintf(&buf, "\nfor _, option := range options {")
fmt.Fprintf(&buf, "\nswitch option := option.(type) {")
fmt.Fprintf(&buf, "\ncase ParseOption:")
fmt.Fprintf(&buf, "\nparseOptions = append(parseOptions, option)")
fmt.Fprintf(&buf, "\ndefault:")
fmt.Fprintf(&buf, "\nreadFileOptions = append(readFileOptions, option)")
fmt.Fprintf(&buf, "\n}")
fmt.Fprintf(&buf, "\n}")
fmt.Fprintf(&buf, "\n\nf, err := fs.Open(path, readFileOptions...)")
fmt.Fprintf(&buf, "\nif err != nil {")
fmt.Fprintf(&buf, "\nreturn nil, err")
fmt.Fprintf(&buf, "\n}")
fmt.Fprintf(&buf, "\n\ndefer f.Close()")
fmt.Fprintf(&buf, "\nreturn ParseReader(f, parseOptions...)")
fmt.Fprintf(&buf, "\n}")
} else {
fmt.Fprintf(&buf, "\nf, err := fs.Open(path, options...)")
fmt.Fprintf(&buf, "\nif err != nil {")
fmt.Fprintf(&buf, "\nreturn nil, err")
fmt.Fprintf(&buf, "\n}")
fmt.Fprintf(&buf, "\n\ndefer f.Close()")
fmt.Fprintf(&buf, "\nreturn ParseReader(f)")
fmt.Fprintf(&buf, "\n}")
}
if err := codegen.WriteFile(def.Filename, &buf, codegen.WithFormatCode(true)); err != nil {
if cfe, ok := err.(codegen.CodeFormatError); ok {
fmt.Fprint(os.Stderr, cfe.Source())
}
return errors.Wrapf(err, `failed to write to %s`, def.Filename)

fmt.Fprintf(&buf, "\n\n// ReadFileOption describes options that can be passed to ReadFile.")
if !def.ParseOptions {
fmt.Fprintf(&buf, "\n// Currently there are no options available that can be passed to ReadFile, but")
fmt.Fprintf(&buf, "\n// it is provided here for anticipated future additions")
}
return nil
}
fmt.Fprintf(&buf, "\ntype ReadFileOption interface {")
fmt.Fprintf(&buf, "\nreaderOption()")
fmt.Fprintf(&buf, "\n}")

func generateFallbackFile(def definition) error {
var buf bytes.Buffer
fmt.Fprintf(&buf, "// +build !go1.16")
fmt.Fprintf(&buf, "\n\n// Automatically generated by internal/cmd/genreadfile/main.go. DO NOT EDIT")
fmt.Fprintf(&buf, "\n\npackage %s", def.Package)
fmt.Fprintf(&buf, "\n\nimport \"github.com/lestrrat-go/jwx/internal/fs\"")
fmt.Fprintf(&buf, "\n\n// ReadFileOption describes an option that can be passed to `ReadFile`")
fmt.Fprintf(&buf, "\ntype ReadFileOption = fs.OpenOption")
fmt.Fprintf(&buf, "\n\n// ReadFile reads a JWK set at the specified location.")
fmt.Fprintf(&buf, "\n//")
fmt.Fprintf(&buf, "\n\n// for go >= 1.16 where io/fs is supported, you may pass `WithFS()` option")
fmt.Fprintf(&buf, "\n// to provide an alternate location to load the files from to provide an ")
fmt.Fprintf(&buf, "\n// alternate location to load the files from (if you are reading")
fmt.Fprintf(&buf, "\n// this message, your go (or your go doc) is probably running go < 1.16)")
if !def.ParseOptions {
fmt.Fprintf(&buf, "\nfunc ReadFile(path string) (%s, error) {", def.ReturnType)
fmt.Fprintf(&buf, "\nfunc ReadFile(path string, _ ...ReadFileOption) (%s, error) {", def.ReturnType)
} else {
fmt.Fprintf(&buf, "\nfunc ReadFile(path string, options ...ReadFileOption) (%s, error) {", def.ReturnType)
fmt.Fprintf(&buf, "\nvar parseOptions []ParseOption")
Expand All @@ -152,10 +81,11 @@ func generateFallbackFile(def definition) error {
fmt.Fprintf(&buf, "\nparseOptions = append(parseOptions, option)")
fmt.Fprintf(&buf, "\n}")
fmt.Fprintf(&buf, "\n}")
fmt.Fprintf(&buf, "\n")
}
fmt.Fprintf(&buf, "\nf, err := os.Open(path)")
fmt.Fprintf(&buf, "\nif err != nil {")
fmt.Fprintf(&buf, "\nreturn nil, errors.Wrapf(err, `failed to open %%s`, path)")
fmt.Fprintf(&buf, "\nreturn nil, err")
fmt.Fprintf(&buf, "\n}")
fmt.Fprintf(&buf, "\n\ndefer f.Close()")
if def.ParseOptions {
Expand All @@ -164,11 +94,11 @@ func generateFallbackFile(def definition) error {
fmt.Fprintf(&buf, "\nreturn ParseReader(f)")
}
fmt.Fprintf(&buf, "\n}")
if err := codegen.WriteFile(def.FallbackFilename, &buf, codegen.WithFormatCode(true)); err != nil {
if err := codegen.WriteFile(def.Filename, &buf, codegen.WithFormatCode(true)); err != nil {
if cfe, ok := err.(codegen.CodeFormatError); ok {
fmt.Fprint(os.Stderr, cfe.Source())
}
return errors.Wrapf(err, `failed to write to %s`, def.FallbackFilename)
return errors.Wrapf(err, `failed to write to %s`, def.Filename)
}
return nil
}
41 changes: 0 additions & 41 deletions internal/fs/fs.go

This file was deleted.

17 changes: 0 additions & 17 deletions internal/fs/fs_go1.15.go

This file was deleted.

21 changes: 0 additions & 21 deletions internal/fs/options.go

This file was deleted.

34 changes: 8 additions & 26 deletions jwe/io.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
// +build go1.16

// Automatically generated by internal/cmd/genreadfile/main.go. DO NOT EDIT

package jwe

import "github.com/lestrrat-go/jwx/internal/fs"

// ReadFileOption describes an option that can be passed to `ReadFile`
type ReadFileOption = fs.OpenOption

// LocalFS is used to explicitly tell ReadFile to read from the
// local file system
type LocalFS = fs.Local
import "os"

// WithFS creates an option that specifies where to load files from
// when `ReadFile()` is called. For example, you can specify an
// instance of `embed.FS` to load files from an embedded file
// system in a compiled binary.
func WithFS(v fs.FS) ReadFileOption {
return fs.WithFS(v)
// ReadFileOption describes options that can be passed to ReadFile.
// Currently there are no options available that can be passed to ReadFile, but
// it is provided here for anticipated future additions
type ReadFileOption interface {
readerOption()
}

// ReadFile reads a JWK set at the specified location.
//
// For go >= 1.16 where io/fs is supported, you may pass `WithFS()` option
// to provide an alternate location to load the files from. This means
// you can embed your JWK in your compiled program and read it back from
// the embedded resource.
//
// By default files are read from the local file system by using `os.Open`
func ReadFile(path string, options ...ReadFileOption) (*Message, error) {
f, err := fs.Open(path, options...)
func ReadFile(path string, _ ...ReadFileOption) (*Message, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
Expand Down
32 changes: 0 additions & 32 deletions jwe/io_go1.15.go

This file was deleted.

34 changes: 8 additions & 26 deletions jwk/io.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
// +build go1.16

// Automatically generated by internal/cmd/genreadfile/main.go. DO NOT EDIT

package jwk

import "github.com/lestrrat-go/jwx/internal/fs"

// ReadFileOption describes an option that can be passed to `ReadFile`
type ReadFileOption = fs.OpenOption

// LocalFS is used to explicitly tell ReadFile to read from the
// local file system
type LocalFS = fs.Local
import "os"

// WithFS creates an option that specifies where to load files from
// when `ReadFile()` is called. For example, you can specify an
// instance of `embed.FS` to load files from an embedded file
// system in a compiled binary.
func WithFS(v fs.FS) ReadFileOption {
return fs.WithFS(v)
// ReadFileOption describes options that can be passed to ReadFile.
// Currently there are no options available that can be passed to ReadFile, but
// it is provided here for anticipated future additions
type ReadFileOption interface {
readerOption()
}

// ReadFile reads a JWK set at the specified location.
//
// For go >= 1.16 where io/fs is supported, you may pass `WithFS()` option
// to provide an alternate location to load the files from. This means
// you can embed your JWK in your compiled program and read it back from
// the embedded resource.
//
// By default files are read from the local file system by using `os.Open`
func ReadFile(path string, options ...ReadFileOption) (Set, error) {
f, err := fs.Open(path, options...)
func ReadFile(path string, _ ...ReadFileOption) (Set, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit bc69aee

Please sign in to comment.