Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: fix and cleanup uninitialized checks for container elements when has len(fix #20272) #20279

Merged
merged 2 commits into from
Dec 28, 2023

Conversation

shove70
Copy link
Contributor

@shove70 shove70 commented Dec 27, 2023

  1. Fixed Segmentation fault, caused indirectly, by using mut x := []X{ len: positive }, where X is sumtype or interface #20272
  2. A minor cleanup
  3. Optimized test cases

The original "uninitialized checks" for sum_type and interface don't handle generics and don't recursively check child elements, so the uninitialized checks for ref, sum_type, and interface are consolidated into a single recursive method.

import x.json2

pub enum ComponentType {
	action_row         = 1
	text_input         = 4
}

pub fn (ct ComponentType) build() json2.Any {
	return json2.Any(int(ct))
}

pub interface Component {
	is_component()
}

pub struct ActionRow {
pub:
	components []Component
}

fn (_ ActionRow) is_component() {}

pub fn ActionRow.parse(j map[string]json2.Any) !ActionRow {
	return ActionRow{
		components: (j['components']! as []json2.Any).map(Component.parse(it)!)
	}
}

pub struct TextInput {
pub:
	custom_id string @[required]
	value ?string
}

fn (_ TextInput) is_component() {}

pub fn TextInput.parse(j json2.Any) !TextInput {
	match j {
		map[string]json2.Any {
			return TextInput{
				custom_id: j['custom_id']! as string
				value: if s := j['value'] {
					?string(s as string)
				} else {
					none
				}
			}
		}
		else {
			return error('expected text input to be object, got ${j.type_name()}')
		}
	}
}

pub fn Component.parse(j json2.Any) !Component {
	match j {
		map[string]json2.Any {
			typ := unsafe { ComponentType(j['type']!.int()) }
			match typ {
				.action_row {
					return ActionRow.parse(j)!
				}
				.text_input {
					return TextInput.parse(j)!
				}
			}
		}
		else {
			return error('expected component to be object, got ${j.type_name()}')
		}
	}
}

pub fn maybe_map[T, X](a []T, f fn(T) !X) ![]X {
	mut r := []X{len: a.len}
	for v in a {
		r << f(v)!
	}
	return r
}

pub struct ModalSubmitData {
pub:
	// the custom_id of the modal
	custom_id string
	// the values submitted by the user
	components []Component
}

pub fn ModalSubmitData.parse(j json2.Any) !ModalSubmitData {
	match j {
		map[string]json2.Any {
			return ModalSubmitData{
				custom_id: j['custom_id']! as string
				components: maybe_map[json2.Any, Component](j['components']! as []json2.Any, fn (o json2.Any) !Component {
					return Component.parse(o)!
				})!
			}
		}
		else {
			return error('expected modal submit data to be object, got ${j.type_name()}')
		}
	}
}

fn main() {
	d := ModalSubmitData.parse(json2.raw_decode('{"custom_id":"my_cool_modal","components":[{"type":1,"components":[{"value":"cool, really?","type":4,"custom_id":"name"}]}]}') or {
		assert false, 'Should decode without error: ${err}'
		exit(1)
	}) or {
		assert false, 'Should parse without error: ${err}'
		exit(1)
	}
	assert d.components == [
		ActionRow{
			components: [
				TextInput{
					custom_id: 'name'
					value: 'cool, really?'
				}
			]
		}
	]
}

outputs:

a.v:75:11: warning: arrays of interfaces need to be initialized right away, therefore `len:` cannot be used (unless inside `unsafe`)
   73 | 
   74 | pub fn maybe_map[T, X](a []T, f fn(T) !X) ![]X {
   75 |     mut r := []X{len: a.len}
      |              ~~~~
   76 |     for v in a {
   77 |         r << f(v)!

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman spytheman merged commit 2dce525 into vlang:master Dec 28, 2023
54 checks passed
@shove70 shove70 deleted the shove-patch-5 branch December 28, 2023 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Segmentation fault, caused indirectly, by using mut x := []X{ len: positive }, where X is sumtype or interface
2 participants