Skip to content

Commit

Permalink
Merge pull request #13 from zuiwuchang/master
Browse files Browse the repository at this point in the history
option for loader
  • Loading branch information
liuzl authored Feb 25, 2022
2 parents f8cb162 + ee1baf0 commit aa3aa1e
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 17 deletions.
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/liuzl/gocc

go 1.16

require (
github.com/liuzl/cedar-go v0.0.0-20170805034717-80a9c64b256d // indirect
github.com/liuzl/da v0.0.0-20180704015230-14771aad5b1d
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/liuzl/cedar-go v0.0.0-20170805034717-80a9c64b256d h1:qSmEGTgjkESUX5kPMSGJ4pcBUtYVDdkNzMrjQyvRvp0=
github.com/liuzl/cedar-go v0.0.0-20170805034717-80a9c64b256d/go.mod h1:x7SghIWwLVcJObXbjK7S2ENsT1cAcdJcPl7dRaSFog0=
github.com/liuzl/da v0.0.0-20180704015230-14771aad5b1d h1:hTRDIpJ1FjS9ULJuEzu69n3qTgc18eI+ztw/pJv47hs=
github.com/liuzl/da v0.0.0-20180704015230-14771aad5b1d/go.mod h1:7xD3p0XnHvJFQ3t/stEJd877CSIMkH/fACVWen5pYnc=
17 changes: 17 additions & 0 deletions loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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)
}
58 changes: 41 additions & 17 deletions opencc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gocc

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
Expand All @@ -16,21 +15,25 @@ import (
)

var (
// Dir is the parent dir for config and dictionary
Dir = flag.String("dir", defaultDir(), "dict dir")
configDir = "config"
dictDir = "dictionary"
)
var defaultDir string

func defaultDir() string {
func DefaultDir() string {
if defaultDir == `` {
return defaultDir
}
if runtime.GOOS == "windows" {
return `C:\gocc\`
defaultDir = `C:\gocc\`
return defaultDir
}
if goPath, ok := os.LookupEnv("GOPATH"); ok {
return goPath + "/src/github.com/liuzl/gocc/"
defaultDir = goPath + "/src/github.com/liuzl/gocc/"
} else {
return `/usr/local/share/gocc/`
defaultDir = `/usr/local/share/gocc/`
}
return defaultDir
}

// Group holds a sequence of dicts
Expand Down Expand Up @@ -58,12 +61,17 @@ 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) (*OpenCC, error) {
func New(conversion string, option ...Option) (*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()
err := cc.initDict(&opts)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -107,13 +115,23 @@ func (cc *OpenCC) Convert(in string) (string, error) {
}
return in, nil
}

func (cc *OpenCC) initDict() error {
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 {
if cc.Conversion == "" {
return fmt.Errorf("conversion is not set")
}
configFile := filepath.Join(*Dir, configDir, cc.Conversion+".json")
body, err := ioutil.ReadFile(configFile)
configFile := cc.join(opts.dir, configDir, cc.Conversion+".json")
rc, err := opts.loader.Open(configFile)
if err != nil {
return err
}
body, err := ioutil.ReadAll(rc)
rc.Close()
if err != nil {
return err
}
Expand All @@ -137,7 +155,7 @@ func (cc *OpenCC) initDict() 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)
group, err := cc.addDictChain(dict, opts)
if err != nil {
return err
}
Expand All @@ -157,7 +175,7 @@ func (cc *OpenCC) initDict() error {
return nil
}

func (cc *OpenCC) addDictChain(d map[string]interface{}) (*Group, error) {
func (cc *OpenCC) addDictChain(d map[string]interface{}, opts *options) (*Group, error) {
t, has := d["type"]
if !has {
return nil, fmt.Errorf("type not found in %+v", d)
Expand All @@ -177,7 +195,7 @@ func (cc *OpenCC) addDictChain(d map[string]interface{}) (*Group, error) {

for _, dict := range dicts {
if d, is := dict.(map[string]interface{}); is {
group, err := cc.addDictChain(d)
group, err := cc.addDictChain(d, opts)
if err != nil {
return nil, err
}
Expand All @@ -192,7 +210,13 @@ func (cc *OpenCC) addDictChain(d map[string]interface{}) (*Group, error) {
if !has {
return nil, fmt.Errorf("no file field found")
}
daDict, err := da.BuildFromFile(filepath.Join(*Dir, dictDir, file.(string)))
filename := cc.join(opts.dir, dictDir, file.(string))
rc, err := opts.loader.Open(filename)
if err != nil {
return nil, err
}
daDict, err := da.Build(rc)
rc.Close()
if err != nil {
return nil, err
}
Expand Down
42 changes: 42 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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
}
})
}

0 comments on commit aa3aa1e

Please sign in to comment.