Skip to content

Commit

Permalink
feat: add regexp config type
Browse files Browse the repository at this point in the history
  • Loading branch information
langhuihui committed Dec 11, 2023
1 parent 46130a1 commit 35741fc
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 160 deletions.
112 changes: 36 additions & 76 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Config struct {
}

var durationType = reflect.TypeOf(time.Duration(0))
var regexpType = reflect.TypeOf(Regexp{})

type Plugin interface {
// 可能的入参类型:FirstConfig 第一次初始化配置,Config 后续配置更新,SE系列(StateEvent)流状态变化事件
Expand Down Expand Up @@ -118,9 +119,10 @@ func (config *Config) Parse(s any, prefix ...string) {
if len(prefix) > 0 { // 读取环境变量
envKey := strings.Join(prefix, "_")
if envValue := os.Getenv(envKey); envValue != "" {
yaml.Unmarshal([]byte(fmt.Sprintf("env: %s", envValue)), config)
envv := config.assign(strings.ToLower(prefix[0]), envValue)
config.Env = envv.Interface()
config.Value = config.Env
config.Ptr.Set(reflect.ValueOf(config.Env))
config.Ptr.Set(envv)
}
}
if t.Kind() == reflect.Struct {
Expand Down Expand Up @@ -303,7 +305,17 @@ func (config *Config) schema(index int) (r any) {
p.Description = "已使用全局配置中的值"
}
p.Enum = config.Enum
if config.Ptr.Type() == durationType {
switch config.Ptr.Type() {
case regexpType:
p.Type = "string"
p.Component = "Input"
p.DecoratorProps["addonAfter"] = "正则表达式"
str := config.Value.(Regexp).String()
p.ComponentProps = map[string]any{
"placeholder": str,
}
p.Default = str
case durationType:
p.Type = "string"
p.Component = "Input"
str := config.Value.(time.Duration).String()
Expand All @@ -312,11 +324,11 @@ func (config *Config) schema(index int) (r any) {
}
p.Default = str
p.DecoratorProps["addonAfter"] = "时间,单位:s,m,h,d,例如:100ms, 10s, 4m, 1h"
} else {
default:
switch config.Ptr.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64:
p.Type = "number"
p.Component = "NumberPicker"
p.Component = "InputNumber"
p.ComponentProps = map[string]any{
"placeholder": config.Value,
}
Expand Down Expand Up @@ -417,6 +429,8 @@ func (config *Config) schema(index int) (r any) {
}
p.Default = children
return p
default:

}
}
if len(p.Enum) > 0 {
Expand Down Expand Up @@ -449,31 +463,15 @@ func (config *Config) GetFormily() (r Object) {
return
}

// func (config *Config) GetModify() map[string]any {
// m := make(map[string]any)
// for k, v := range config.props {
// if v.props != nil {
// if vv := v.GetModify(); vv != nil {
// m[k] = vv
// }
// } else if v.Modify != nil {
// m[k] = v.Modify
// }
// }
// if len(m) > 0 {
// return m
// }
// return nil
// }

var regexPureNumber = regexp.MustCompile(`^\d+$`)

func (config *Config) assign(k string, v any) (target reflect.Value) {
ft := config.Ptr.Type()

source := reflect.ValueOf(v)

if ft == durationType {
switch ft {
case durationType:
target = reflect.New(ft).Elem()
if source.Type() == durationType {
target.Set(source)
Expand All @@ -492,59 +490,21 @@ func (config *Config) assign(k string, v any) (target reflect.Value) {
os.Exit(1)
}
}
return
case regexpType:
target = reflect.New(ft).Elem()
regexpStr := source.String()
target.Set(reflect.ValueOf(Regexp{regexp.MustCompile(regexpStr)}))
default:
tmpStruct := reflect.StructOf([]reflect.StructField{
{
Name: strings.ToUpper(k),
Type: ft,
},
})
tmpValue := reflect.New(tmpStruct)
tmpByte, _ := yaml.Marshal(map[string]any{k: v})
yaml.Unmarshal(tmpByte, tmpValue.Interface())
target = tmpValue.Elem().Field(0)
}

tmpStruct := reflect.StructOf([]reflect.StructField{
{
Name: strings.ToUpper(k),
Type: ft,
},
})
tmpValue := reflect.New(tmpStruct)
tmpByte, _ := yaml.Marshal(map[string]any{k: v})
yaml.Unmarshal(tmpByte, tmpValue.Interface())
return tmpValue.Elem().Field(0)
// switch target.Kind() {
// case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// target.SetUint(uint64(source.Int()))
// case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
// target.SetInt(source.Int())
// case reflect.Float32, reflect.Float64:
// if source.CanFloat() {
// target.SetFloat(source.Float())
// } else {
// target.SetFloat(float64(source.Int()))
// }
// case reflect.Map:

// case reflect.Slice:
// var s reflect.Value
// if source.Kind() == reflect.Slice {
// l := source.Len()
// s = reflect.MakeSlice(ft, l, source.Cap())
// for i := 0; i < l; i++ {
// fv := source.Index(i)
// item := s.Index(i)
// if child, ok := fv.Interface().(map[string]any); ok {
// panic(child)
// // item.Set(child.CreateElem(ft.Elem()))
// } else if fv.Kind() == reflect.Interface {
// item.Set(reflect.ValueOf(fv.Interface()).Convert(item.Type()))
// } else {
// item.Set(fv)
// }
// }
// } else {
// //值是单值,但类型是数组,默认解析为一个元素的数组
// s = reflect.MakeSlice(ft, 1, 1)
// s.Index(0).Set(source)
// }
// target.Set(s)
// default:
// if source.IsValid() {
// target.Set(source.Convert(ft))
// }
// }
return
}
44 changes: 44 additions & 0 deletions config/regexp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package config

import (
"regexp"

"gopkg.in/yaml.v3"
)

type Regexp struct {
*regexp.Regexp
}

func (r *Regexp) UnmarshalYAML(node *yaml.Node) error {
r.Regexp = regexp.MustCompile(node.Value)
return nil
}

func (r *Regexp) MarshalYAML() (interface{}, error) {
if r.Regexp == nil {
return "", nil
}
return r.String(), nil
}

func (r *Regexp) MarshalJSON() ([]byte, error) {
if r.Regexp == nil {
return []byte(`""`), nil
}
return []byte(`"` + r.String() + `"`), nil
}

func (r *Regexp) UnmarshalJSON(b []byte) error {
if len(b) == 0 {
return nil
}
if b[0] == '"' {
b = b[1:]
}
if b[len(b)-1] == '"' {
b = b[:len(b)-1]
}
r.Regexp = regexp.MustCompile(string(b))
return nil
}
43 changes: 21 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ module m7s.live/engine/v4
go 1.20

require (
github.com/bluenviron/mediacommon v1.0.0
github.com/bluenviron/gortsplib/v4 v4.6.2
github.com/bluenviron/mediacommon v1.5.1
github.com/cnotch/ipchub v1.1.0
github.com/google/uuid v1.3.0
github.com/google/uuid v1.4.0
github.com/logrusorgru/aurora/v4 v4.0.0
github.com/mcuadros/go-defaults v1.2.0
github.com/pion/rtp v1.8.1
github.com/pion/webrtc/v3 v3.2.14
github.com/q191201771/naza v0.30.11
github.com/quic-go/quic-go v0.37.4
github.com/shirou/gopsutil/v3 v3.23.7
go.uber.org/zap v1.24.0
golang.org/x/net v0.14.0
golang.org/x/sync v0.2.0
github.com/pion/rtp v1.8.3
github.com/pion/webrtc/v3 v3.2.20
github.com/q191201771/naza v0.30.48
github.com/quic-go/quic-go v0.38.1
github.com/shirou/gopsutil/v3 v3.23.8
go.uber.org/zap v1.26.0
golang.org/x/net v0.19.0
golang.org/x/sync v0.3.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -25,24 +26,22 @@ require (
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a // indirect
github.com/onsi/ginkgo/v2 v2.9.5 // indirect
github.com/pion/randutil v0.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/errors v0.9.1
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect
github.com/quic-go/qtls-go1-20 v0.3.1 // indirect
github.com/quic-go/qtls-go1-20 v0.3.3 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/yapingcat/gomedia v0.0.0-20230727105416-c491e66c9d2a
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yapingcat/gomedia v0.0.0-20230905155010-55b9713fcec1
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/tools v0.9.1 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)
Loading

0 comments on commit 35741fc

Please sign in to comment.