Jump to content

[golang] go-ini overwrite from struct

Zalosath
Go to solution Solved by Zalosath,

Solved, 

gIni.Append([]byte(s.additionalValues))

was the problem. The Append function has a bug (https://github.com/go-ini/ini/issues/324) that causes the problem.

Hi,

I'm working with go-ini and golang (still relatively new to golang as a whole, but getting to grips with it). I have this function:
 

func SaveSettings(filePathToLoadFrom string, overrideUseConfig bool) error {

	ini.PrettyFormat = false

	filePath := filepath.Join(s.path, "path.ini")
	err := ensureFilePath(filePath)
	if err != nil {
		return err
	}

	gIni, err := ini.LoadSources(iniOpts, filePathToLoadFrom)
	if err != nil {
		return err
	}

	if overrideUseConfig {
		err = gIni.MapTo(&s.GSettings)
		if err != nil {
			return err
		}
	}

	err = gIni.ReflectFrom(&s.GSettings)
	if err != nil {
		return err
	}

	gIni.Append([]byte(s.additionalValues))

	err = gIni.SaveTo(filepath.Join(s.path, "path.ini"))
	if err != nil {
		return err
	}

	return nil
}

Where iniopts is:

var iniOpts = ini.LoadOptions{
	AllowShadows: false,
}

 

This is creating a problem though. ReflectFrom isn't overwriting the settings, it just ignores them, this only happens if the AllowShadows flag is set to false, which is expected. How can I change this behaviour so it overwrites the data in gIni without creating duplicates?

Main PC [ CPU AMD Ryzen 9 7900X3D with H150i ELITE CAPPELIX  GPU Nvidia 3090 FE  MBD ASUS ROG STRIX X670E-A  RAM Corsair Dominator Platinum 64GB@5600MHz  PSU HX1000i  Case Lian Li PC-O11 Dynamic  Monitor LG UltraGear 1440p 32" Nano IPS@180Hz  Keyboard Keychron Q6 with Kailh Box Switch Jade  Mouse Logitech G Pro Superlight  Microphone Shure SM7B with Cloudlifter & GoXLR ]

 

Server [ CPU AMD Ryzen 5 5600G  GPU Intel ARC A380  RAM Corsair VEGEANCE LPX 64GB  Storage 16TB EXOS ]

 

Phone [ Google Pixel 8 Pro, 256GB, Snow ]

Link to comment
Share on other sites

Link to post
Share on other sites

Solved, 

gIni.Append([]byte(s.additionalValues))

was the problem. The Append function has a bug (https://github.com/go-ini/ini/issues/324) that causes the problem.

Main PC [ CPU AMD Ryzen 9 7900X3D with H150i ELITE CAPPELIX  GPU Nvidia 3090 FE  MBD ASUS ROG STRIX X670E-A  RAM Corsair Dominator Platinum 64GB@5600MHz  PSU HX1000i  Case Lian Li PC-O11 Dynamic  Monitor LG UltraGear 1440p 32" Nano IPS@180Hz  Keyboard Keychron Q6 with Kailh Box Switch Jade  Mouse Logitech G Pro Superlight  Microphone Shure SM7B with Cloudlifter & GoXLR ]

 

Server [ CPU AMD Ryzen 5 5600G  GPU Intel ARC A380  RAM Corsair VEGEANCE LPX 64GB  Storage 16TB EXOS ]

 

Phone [ Google Pixel 8 Pro, 256GB, Snow ]

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×