Exemple #1
0
Fichier : type.go Projet : kego/ke
func (t *Type) GetReflectInterface(ctx context.Context) (reflect.Type, bool) {
	ifunc, ok := jsonctx.FromContext(ctx).GetInterfaceFunc(t.Id.Package, t.Id.Name)
	if !ok {
		return nil, false
	}
	return ifunc(), true
}
Exemple #2
0
func comparePackageHash(ctx context.Context, path string) (changes bool, err error) {

	scache := sysctx.FromContext(ctx)
	spi, ok := scache.Get(path)
	if !ok {
		return false, kerr.New("NHXWLPHCHL", "%s not found in sys ctx", path)
	}

	for _, aliasPath := range spi.Aliases {
		changes, err := comparePackageHash(ctx, aliasPath)
		if err != nil {
			return false, kerr.Wrap("DGJTLHQOCQ", err)
		}
		if changes {
			return true, nil
		}
	}

	jcache := jsonctx.FromContext(ctx)
	jpi, ok := jcache.Packages.Get(path)
	if !ok {
		return true, nil
	}

	// pcache.Environment.Hash is computed after parsing all the data files.
	// h.Hash is in generated.go (jsonctx.InitPackage), and correct when the types were generated.
	if jpi.Hash != spi.Hash {
		return true, nil
	}
	return false, nil
}
Exemple #3
0
Fichier : tests.go Projet : kego/ke
func (c *ContextBuilder) initJson() *jsonctx.JsonCache {
	jcache := jsonctx.FromContextOrNil(c.ctx)
	if jcache == nil {
		c.ctx = jsonctx.ManualContext(c.ctx)
		jcache = jsonctx.FromContext(c.ctx)
	}
	return jcache
}
Exemple #4
0
Fichier : type.go Projet : kego/ke
func (t *Type) GetReflectType(ctx context.Context) (reflect.Type, bool) {
	nf, df, ok := jsonctx.FromContext(ctx).GetNewFunc(t.Id.Package, t.Id.Name)
	if !ok {
		return nil, false
	}
	rt := reflect.TypeOf(nf())
	if df != nil || t.Interface {
		rt = rt.Elem()
	}
	return rt, true
}
Exemple #5
0
func GetEmbedEditable(ctx context.Context, node *node.Node, embed *system.Reference) (editable.Editable, error) {

	if node == nil || node.Null || node.Missing {
		return nil, nil
	}

	if *node.Type.Id == *embed {
		return GetEditable(ctx, node), nil
	}

	jcache := jsonctx.FromContext(ctx)
	nf, df, ok := jcache.GetNewFunc(embed.Package, embed.Name)
	if !ok {
		return nil, kerr.New("DGWDERFPVV", "Can't find %s in jsonctx", embed.String())
	}
	t := reflect.TypeOf(nf())
	if df != nil {
		t = t.Elem()
	}

	v := node.Val
	for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
		v = v.Elem()
	}

	var field reflect.Value
	for i := 0; i < v.Type().NumField(); i++ {
		f := v.Type().Field(i)
		if f.Anonymous && f.Type == t {
			field = v.Field(i)
			break
		}
	}
	if field == (reflect.Value{}) {
		return nil, kerr.New("UDBOWYUBER", "Can't find %s field in struct", t)
	}

	// This is the recommended method of presenting an custom editor.
	if ed, ok := field.Interface().(editable.Editable); ok {
		return ed, nil
	}

	editors := clientctx.FromContext(ctx)

	// Don't do this. Implement the Editable interface instead. We can't do this
	// for system types so we use this method instead.
	if e, ok := editors.Get(embed.String()); ok {
		return e, nil
	}

	return nil, nil

}
Exemple #6
0
func GetNewFromTypeField(ctx context.Context, in Packed, iface bool, ifacePackage, ifaceName string) (newFunc func() interface{}, derefFunc func(interface{}) interface{}, err error) {
	t, ok := in.Map()["type"]
	if !ok {
		return nil, nil, kerr.New("RXEPCCGFKV", "Type field not found.")
	}
	tr := new(Reference)
	if err := tr.Unpack(ctx, t, false); err != nil {
		return nil, nil, kerr.Wrap("MVOYONPYER", err)
	}
	jctx := jsonctx.FromContext(ctx)
	nf, df, ok := jctx.GetNewFunc(tr.Package, tr.Name)
	if ok {
		return nf, df, nil
	}
	if iface && ifacePackage != "" && ifaceName != "" {
		r := NewReference(ifacePackage, ifaceName)
		nf, ok := jsonctx.FromContext(ctx).GetDummyFunc(r.Package, r.Name)
		if ok {
			// TODO: fix this
			return nf, nil, nil
		}
	}
	return nil, nil, kerr.New("BQIJMVGJDT", "NewFunc %s not found.", tr.String())
}