Repository: ollama/ollama
File: server/images.go (Go)

Code before:
					floatVal, err := strconv.ParseFloat(val, 32)
					if err != nil {
						return nil, fmt.Errorf("invalid float value %s", val)
					}

					field.SetFloat(floatVal)
				case reflect.Int:
					intVal, err := strconv.ParseInt(val, 10, 0)
					if err != nil {
						return nil, fmt.Errorf("invalid int value %s", val)
					}

					field.SetInt(intVal)
				case reflect.Bool:
					boolVal, err := strconv.ParseBool(val)
					if err != nil {
						return nil, fmt.Errorf("invalid bool value %s", val)
					}

					field.SetBool(boolVal)
				case reflect.String:
					field.SetString(val)
				case reflect.Slice:
					re := regexp.MustCompile(`"(.*?)"`) // matches everything enclosed in quotes
					vals := re.FindAllStringSubmatch(val, -1)
					var sliceVal []string
					for _, v := range vals {
						sliceVal = append(sliceVal, v[1]) // v[1] is the captured group, v[0] is the entire match
					}
					field.Set(reflect.ValueOf(sliceVal))
				default:
					return nil, fmt.Errorf("unknown type %s for %s", field.Kind(), key)
				}
			}
		}
	}

	bts, err := json.Marshal(opts)
	if err != nil {
		return nil, err
	}

	return bytes.NewReader(bts), nil
}

func getLayerDigests(layers []*LayerReader) ([]string, error) {
	var digests []string
	for _, l := range layers {
		if l.Digest == "" {
			return nil, fmt.Errorf("layer is missing a digest")
		}

Review comment (bug):
It seems you're using regex to get all the values wrapped in double quotes? This likely won't work since the parser parses out a single parameter value from the line

e.g.

This is parsed as `parameters[stop]=[]string{"foo"}`
```
PARAMETER stop "foo" "bar" "baz"
```

While this is parsed as `parameters[stop]=[]string{"foo bar baz"}`
```
PARAMETER stop foo "bar baz"
```

Clearly this is the wrong behaviour but implementing a parser that can handle this correctly is not worthwhile right now. 

What I have in mind is something like this to create `parameters[stop]=[]string{"foo", "bar baz"}`

```
PARAMETER stop foo
PARAMETER stop "bar baz"
```

This won't work immediately either since values are inserted into a `map[string]string` so the last value will override all previous values

Code after:
					floatVal, err := strconv.ParseFloat(vals[0], 32)
					if err != nil {
						return nil, fmt.Errorf("invalid float value %s", vals)
					}

					field.SetFloat(floatVal)
				case reflect.Int:
					intVal, err := strconv.ParseInt(vals[0], 10, 0)
					if err != nil {
						return nil, fmt.Errorf("invalid int value %s", vals)
					}

					field.SetInt(intVal)
				case reflect.Bool:
					boolVal, err := strconv.ParseBool(vals[0])
					if err != nil {
						return nil, fmt.Errorf("invalid bool value %s", vals)
					}

					field.SetBool(boolVal)
				case reflect.String:
					field.SetString(vals[0])
				case reflect.Slice:
					field.Set(reflect.ValueOf(vals))
				default:
					return nil, fmt.Errorf("unknown type %s for %s", field.Kind(), key)
				}
			}
		}
	}

	bts, err := json.Marshal(opts)
	if err != nil {
		return nil, err
	}

	return bytes.NewReader(bts), nil
}

func getLayerDigests(layers []*LayerReader) ([]string, error) {
	var digests []string
	for _, l := range layers {
		if l.Digest == "" {
			return nil, fmt.Errorf("layer is missing a digest")
		}
		digests = append(digests, l.Digest)
	}
	return digests, nil
}

// CreateLayer creates a Layer object from a given file