From f1590118c61560fbf23e9369c79e393fbdbc6730 Mon Sep 17 00:00:00 2001 From: vzz64 <120877801+vzz64@users.noreply.github.com> Date: Thu, 22 Dec 2022 10:01:12 +0800 Subject: [PATCH] embed config files at compile time --- cmd/Makefile | 3 --- config_test.go | 3 +-- loader.go | 17 -------------- opencc.go | 60 ++++++++++++++------------------------------------ option.go | 42 ----------------------------------- 5 files changed, 18 insertions(+), 107 deletions(-) delete mode 100644 loader.go delete mode 100644 option.go diff --git a/cmd/Makefile b/cmd/Makefile index 76060ff..d16b9d9 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -26,7 +26,6 @@ BINARY_NAME=gocc PREFIX?=/usr/local INSTALL_BIN=$(PREFIX)/bin -INSTALL_DATA=$(PREFIX)/share/gocc build: $(GOGET) @@ -37,6 +36,4 @@ clean: install: build @mkdir -p $(INSTALL_BIN) - @mkdir -p $(INSTALL_DATA) cp $(BINARY_NAME) $(INSTALL_BIN) - cp -fr ../config ../dictionary $(INSTALL_DATA) diff --git a/config_test.go b/config_test.go index ad10a21..43872a0 100644 --- a/config_test.go +++ b/config_test.go @@ -2,7 +2,6 @@ package gocc import ( "encoding/json" - "io/ioutil" "testing" ) @@ -12,7 +11,7 @@ func TestConfig(t *testing.T) { "config/t2s.json", } for _, c := range cases { - body, err := ioutil.ReadFile(c) + body, err := config.ReadFile(c) if err != nil { t.Error(err) } diff --git a/loader.go b/loader.go deleted file mode 100644 index a7bf023..0000000 --- a/loader.go +++ /dev/null @@ -1,17 +0,0 @@ -package gocc - -import ( - "io" - "os" -) - -type Loader interface { - Open(filepath string) (io.ReadCloser, error) -} - -type OSLoader struct { -} - -func (OSLoader) Open(filepath string) (io.ReadCloser, error) { - return os.Open(filepath) -} diff --git a/opencc.go b/opencc.go index 3c10e12..beefedc 100644 --- a/opencc.go +++ b/opencc.go @@ -2,39 +2,22 @@ package gocc import ( + "embed" "encoding/json" "fmt" "io/ioutil" - "os" - "path/filepath" + "path" "reflect" - "runtime" "strings" "github.com/liuzl/da" ) -var ( - configDir = "config" - dictDir = "dictionary" -) -var defaultDir string +//go:embed config +var config embed.FS -func DefaultDir() string { - if defaultDir == `` { - return defaultDir - } - if runtime.GOOS == "windows" { - defaultDir = `C:\gocc\` - return defaultDir - } - if goPath, ok := os.LookupEnv("GOPATH"); ok { - defaultDir = goPath + "/src/github.com/liuzl/gocc/" - } else { - defaultDir = `/usr/local/share/gocc/` - } - return defaultDir -} +//go:embed dictionary +var dict embed.FS // Group holds a sequence of dicts type Group struct { @@ -61,17 +44,13 @@ var conversions = map[string]struct{}{ // New construct an instance of OpenCC. // // Supported conversions: s2t, t2s, s2tw, tw2s, s2hk, hk2s, s2twp, tw2sp, t2tw, t2hk -func New(conversion string, option ...Option) (*OpenCC, error) { +func New(conversion string) (*OpenCC, error) { if _, has := conversions[conversion]; !has { return nil, fmt.Errorf("%s not valid", conversion) } - opts := defaultOptions - for _, o := range option { - o.apply(&opts) - } cc := &OpenCC{Conversion: conversion} - err := cc.initDict(&opts) + err := cc.initDict() if err != nil { return nil, err } @@ -115,18 +94,13 @@ func (cc *OpenCC) Convert(in string) (string, error) { } return in, nil } -func (cc *OpenCC) join(root, dir, name string) string { - if root == `` { - return filepath.Join(dir, name) - } - return filepath.Clean(filepath.Join(root, dir, name)) -} -func (cc *OpenCC) initDict(opts *options) error { + +func (cc *OpenCC) initDict() error { if cc.Conversion == "" { return fmt.Errorf("conversion is not set") } - configFile := cc.join(opts.dir, configDir, cc.Conversion+".json") - rc, err := opts.loader.Open(configFile) + configFile := path.Join("config", cc.Conversion+".json") + rc, err := config.Open(configFile) if err != nil { return err } @@ -155,7 +129,7 @@ func (cc *OpenCC) initDict(opts *options) error { if d, ok := v.(map[string]interface{}); ok { if gdict, has := d["dict"]; has { if dict, is := gdict.(map[string]interface{}); is { - group, err := cc.addDictChain(dict, opts) + group, err := cc.addDictChain(dict) if err != nil { return err } @@ -175,7 +149,7 @@ func (cc *OpenCC) initDict(opts *options) error { return nil } -func (cc *OpenCC) addDictChain(d map[string]interface{}, opts *options) (*Group, error) { +func (cc *OpenCC) addDictChain(d map[string]interface{}) (*Group, error) { t, has := d["type"] if !has { return nil, fmt.Errorf("type not found in %+v", d) @@ -195,7 +169,7 @@ func (cc *OpenCC) addDictChain(d map[string]interface{}, opts *options) (*Group, for _, dict := range dicts { if d, is := dict.(map[string]interface{}); is { - group, err := cc.addDictChain(d, opts) + group, err := cc.addDictChain(d) if err != nil { return nil, err } @@ -210,8 +184,8 @@ func (cc *OpenCC) addDictChain(d map[string]interface{}, opts *options) (*Group, if !has { return nil, fmt.Errorf("no file field found") } - filename := cc.join(opts.dir, dictDir, file.(string)) - rc, err := opts.loader.Open(filename) + filename := path.Join("dictionary", file.(string)) + rc, err := dict.Open(filename) if err != nil { return nil, err } diff --git a/option.go b/option.go deleted file mode 100644 index 6bfeaa2..0000000 --- a/option.go +++ /dev/null @@ -1,42 +0,0 @@ -package gocc - -var defaultOptions = options{ - dir: DefaultDir(), - loader: OSLoader{}, -} - -type options struct { - dir string - loader Loader -} - -type Option interface { - apply(*options) -} - -type funcOption struct { - f func(*options) -} - -func (fdo *funcOption) apply(do *options) { - fdo.f(do) -} -func newFuncOption(f func(*options)) *funcOption { - return &funcOption{ - f: f, - } -} -func WithDir(dir string) Option { - return newFuncOption(func(o *options) { - o.dir = dir - }) -} -func WithLoader(loader Loader) Option { - return newFuncOption(func(o *options) { - if loader == nil { - o.loader = OSLoader{} - } else { - o.loader = loader - } - }) -}