Initial commit (code only without large binaries)
This commit is contained in:
81
EdgeNode/internal/js/object.go
Normal file
81
EdgeNode/internal/js/object.go
Normal file
@@ -0,0 +1,81 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package js
|
||||
|
||||
import "rogchap.com/v8go"
|
||||
|
||||
type Object struct {
|
||||
*v8go.Object
|
||||
}
|
||||
|
||||
func NewObject(rawObject *v8go.Object) *Object {
|
||||
return &Object{Object: rawObject}
|
||||
}
|
||||
|
||||
func (this *Object) GetString(key string) (value string, ok bool) {
|
||||
if !this.Has(key) {
|
||||
return "", false
|
||||
}
|
||||
v, err := this.Get(key)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
if v.IsNullOrUndefined() {
|
||||
return "", false
|
||||
}
|
||||
return v.String(), true
|
||||
}
|
||||
|
||||
func (this *Object) GetObject(key string) (value *Object, ok bool) {
|
||||
if !this.Has(key) {
|
||||
return nil, false
|
||||
}
|
||||
val, err := this.Get(key)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if !val.IsObject() {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return NewObject(val.Object()), true
|
||||
}
|
||||
|
||||
func (this *Object) GetStringsArray(key string) (value []string, ok bool) {
|
||||
if !this.Has(key) {
|
||||
return nil, false
|
||||
}
|
||||
val, err := this.Get(key)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
if val.IsNullOrUndefined() {
|
||||
return nil, false
|
||||
}
|
||||
if !val.IsArray() {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
var obj = val.Object()
|
||||
arrayLengthValue, err := obj.Get("length")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if arrayLengthValue.IsUint32() {
|
||||
var arrayLength = arrayLengthValue.Uint32()
|
||||
for i := uint32(0); i < arrayLength; i++ {
|
||||
elementValue, err := obj.GetIdx(i)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if elementValue.IsString() {
|
||||
value = append(value, elementValue.String())
|
||||
}
|
||||
}
|
||||
ok = true
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user