gorums icon indicating copy to clipboard operation
gorums copied to clipboard

Replace ConfigurationFromRaw function with method to replace QuorumSpec

Open meling opened this issue 11 months ago • 0 comments

This is part of generated code:

// ConfigurationFromRaw returns a new Configuration from the given raw configuration and QuorumSpec.
//
// This function may for example be used to "clone" a configuration but install a different QuorumSpec:
//
//	cfg1, err := mgr.NewConfiguration(qspec1, opts...)
//	cfg2 := ConfigurationFromRaw(cfg1.RawConfig, qspec2)
func ConfigurationFromRaw(rawCfg gorums.RawConfiguration, qspec QuorumSpec) (*Configuration, error) {
	// return an error if the QuorumSpec interface is not empty and no implementation was provided.
	var test interface{} = struct{}{}
	if _, empty := test.(QuorumSpec); !empty && qspec == nil {
		return nil, fmt.Errorf("config: missing required QuorumSpec")
	}
	newCfg := &Configuration{
		RawConfiguration: rawCfg,
		qspec:            qspec,
	}
	// initialize the nodes slice
	newCfg.nodes = make([]*Node, newCfg.Size())
	for i, n := range rawCfg {
		newCfg.nodes[i] = &Node{n}
	}
	return newCfg, nil
}

We currently don't use the above method anymore, but it may still be useful, maybe...

We should replace it with something along the lines of this:

// ReplaceQuorumSpec returns a clone of the configuration with a new quorum specification.
//
// This method can be used like this:
//
//	c1, err := NewConfiguration(qspec1, WithNodeList(addrs), opts...)
//	c2, err := c1.ReplaceQuorumSpec(qspec2)
func (c *Configuration) ReplaceQuorumSpec(qspec QuorumSpec) (*Configuration, error) {
	// return an error if the QuorumSpec interface is not empty and no implementation was provided.
	var test any = struct{}{}
	if _, empty := test.(QuorumSpec); !empty && qspec == nil {
		return nil, fmt.Errorf("config: missing required QuorumSpec")
	}
	return &Configuration{
		RawConfiguration: c.RawConfiguration,
		qspec:            qspec,
		nodes:            c.nodes,
//		nodes:            slices.Clone(c.nodes), // I don't think we actually need to clone it, since we should never edit the nodes slice.
	}, nil
}

meling avatar Mar 04 '25 23:03 meling