// Turn a State representation of a Plan into a Plan (used in Plan
// serialization)
Turn a State representation of a Plan into a Plan (used in Plan
serialization)
// Turn a State representation of a Plan into a Plan ( used in Plan // serialization )
func (s State) toPlan() Plan {
	p := newPlan()
	for id := range s {
		p.graph.AddNode(id)
	}
	for id, dval := range s {
		data := dval.(map[string]interface{})
		depsint := (data["meta"].(map[string]interface{}))["dependsOn"]
		var deps []string
		if depstrs, ok := depsint.([]string); ok {
			deps = depstrs
		} else {
			for _, str := range depsint.([]interface{}) {
				deps = append(deps, str.(string))
			}
		}
		for _, dep := range deps {
			p.graph.AddEdge(dep, id)
		}
		var rtype resourceType
		var vals map[string]interface{}
		for k, v := range data {
			if k != "meta" {
				rtype = resourceTypeByName(k)
				vals = v.(map[string]interface{})
				break
			}
		}
		if rtype.baseType == reflect.ValueOf(Plan{}).Type() {
			pres := State(vals).toPlan()
			p.resources[id] = &pres
			continue
		}
		resref := reflect.New(rtype.baseType).Elem()
		fnameMap := extractActualNamesFromTags(rtype.baseType)
		for fname, fval := range vals {
			if fval != nil {
				resref.FieldByName(lookupActualName(fnameMap, fname)).Set(reflect.ValueOf(fval))
			}
		}
		var res Resource
		if rtype.isPtr {
			res = resref.Addr().Interface().(Resource)
		} else {
			res = resref.Interface().(Resource)
		}
		p.resources[id] = res
	}
	return *p
}
func ( s State ) toPlan ( ) Plan { p := newPlan ( ) for id := range s { p . graph . AddNode ( id ) } for id , dval := range s { data := dval . ( map [ string ] interface { } ) depsint := ( data [ " " ] . ( map [ string ] interface { } ) ) [ " " ] var deps [ ] string if depstrs , ok := depsint . ( [ ] string ) ; ok { deps = depstrs } else { for _ , str := range depsint . ( [ ] interface { } ) { deps = append ( deps , str . ( string ) ) } } for _ , dep := range deps { p . graph . AddEdge ( dep , id ) } var rtype resourceType var vals map [ string ] interface { } for k , v := range data { if k != " " { rtype = resourceTypeByName ( k ) vals = v . ( map [ string ] interface { } ) break } } if rtype . baseType == reflect . ValueOf ( Plan { } ) . Type ( ) { pres := State ( vals ) . toPlan ( ) p . resources [ id ] = & pres continue } resref := reflect . New ( rtype . baseType ) . Elem ( ) fnameMap := extractActualNamesFromTags ( rtype . baseType ) for fname , fval := range vals { if fval != nil { resref . FieldByName ( lookupActualName ( fnameMap , fname ) ) . Set ( reflect . ValueOf ( fval ) ) } } var res Resource if rtype . isPtr { res = resref . Addr ( ) . Interface ( ) . ( Resource ) } else { res = resref . Interface ( ) . ( Resource ) } p . resources [ id ] = res } return * p }
Turn a State representation of a Plan into a Plan (used in Plan
serialization)
Turn a State representation of a Plan into a Plan ( used in Plan serialization )
// Note: not using builder because the nodes do not necessarily come back in // their original order and adding dependency edges prior to their nodes // is not supported by the graph. So, we add all nodes and then all edges // First the graph // When we go through JSON, this comes back as []interface{}; otherwise it's []string // Now the resources // One of the entries in the map is the metadata (labeled // "meta"); the other is the Resource but we don't know its // name since it's named by its type // If the resource is a nested Plan, just use the "toPlan" method // Create a new instance of the Resource's struct type and map // tagged names to actual struct field names // Set the fields to the loaded values // If the original Resource was implemented as a pointer type, // take the address before turning the value into a Resource