The documentation for Stack.AvailabilityZones says:
// To specify a different strategy for selecting availability zones override this method.
AvailabilityZones() *[]*string
This is my code that attempts to do this:
stack := awscdk.NewStack(scope, &id, &sprops)
stack.AvailabilityZones = func() *[]*string { // this is line 22
return &[]*string{jsii.String("us-west-1a")}
}
However, I get the error:
[andrew@localhost cdk]$ go run vpc.go common.go
./vpc.go:22:2: cannot assign to stack.AvailabilityZones (value of type func() *[]*string)
How should I be doing this?
I am not sure if I have made a stupid mistake or if I need to do something completely different (what does "override" mean in go which is not OO? edit: or at least not in a way that means I can copy from a python example).
CodePudding user response:
Go has a way to inherit and "override" methods. You inherit by "embedding" another type inside a struct (where you don't name the type); see: Go "inheritance" - using anonymous type in a struct as a method parameter
Your struct will then inherit all the methods from the embedded type. And you can override individual ones by declaring them directly on the struct.
With a simplified example, that would look like:
type MyStack struct {
awscdk.Stack
}
func (s *MyStack) AvailabilityZones() *[]string {
return &[]string{"foo", "bar"}
}
func f() {
// ...
stack := awscdk.NewStack(scope, &id, &sprops)
myStack := &MyStack{Stack: stack}
fmt.Println(myStack.AvailabilityZones())
}
edit: this is what actually worked for me (andrew cooke) (but didn't solve my larger problem, so isn't deeply tested)
type ZoneStack struct {
awscdk.Stack
}
func (stack *ZoneStack) AvailabilityZones() *[]*string {
return &[]*string{jsii.String("us-west-1a")}
}
...
zstack := ZoneStack{}
awscdk.NewStack_Override(&zstack, scope, &id, &sprops)
(the original answer above compiles, but gives runtime errors that seem to be related to JS serialization?!)