go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/pkg/incrutil/var.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package incrutil 9 10 import ( 11 "github.com/wcharczuk/go-incr" 12 ) 13 14 func Var[A any](scope incr.Scope, value A) VarIncr[A] { 15 return incr.WithinScope(scope, &varIncr[A]{ 16 VarIncr: incr.Var(scope, value), 17 }) 18 } 19 20 type VarIncr[A any] interface { 21 incr.VarIncr[A] 22 IRawValue 23 ISetRawValue 24 } 25 26 type varIncr[A any] struct { 27 incr.VarIncr[A] 28 } 29 30 func (v *varIncr[A]) RawValue() any { 31 return v.Value() 32 } 33 34 func (v *varIncr[A]) SetRawValue(value any) error { 35 var typed A 36 if err := CastAny(value, &typed); err != nil { 37 return err 38 } 39 v.Set(typed) 40 return nil 41 } 42 43 func (v *varIncr[A]) SetRawVarValue(value any) error { 44 var typed A 45 if err := CastAny(value, &typed); err != nil { 46 return err 47 } 48 incr.ExpertVar(v.VarIncr).SetInternalValue(typed) 49 return nil 50 } 51 52 func (v *varIncr[A]) String() string { 53 return v.VarIncr.Node().String() 54 }